Know if a object member is a method

Luca lucafbb at gmail.com
Mon Sep 1 05:45:36 EDT 2008


On Mon, Sep 1, 2008 at 11:35 AM, Steven D'Aprano
<steven at remove.this.cybersource.com.au> wrote:
> That's relatively fragile, since such names aren't reserved in any way.
> It's easy to fool a name comparison check with an accidental name
> collision:
>
>>>> class function(object):  # not a reserved name
> ...     pass
> ...
>>>> x = function()
>>>> type(x).__name__
> 'function'
>>>> x()  # must be a function then...
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> TypeError: 'function' object is not callable
>
>
> But not so easy to fool a type check:
>
>>>> type(x) == new.function
> False
>
> Of course that's not bullet-proof either. I leave it as an exercise to
> discover how you might break that piece of code.
>

Ok, so...

What is the best way to do this? The "most pythonic"?


-- 
-- luca



More information about the Python-list mailing list