Connecting Tech Pros Worldwide Forums | Help | Site Map

Re: Know if a object member is a method

Manuel Ebert
Guest
 
Posts: n/a
#1: Sep 1 '08
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi Luca,

use type(something).__name__ , e.g.
Quote:
Quote:
Quote:
>>def x():
>>> pass
>>class C:
>>> pass
>>c = C()
>>type(x).__name__ == 'function'
True
Quote:
Quote:
>type(C).__name__ == 'classobj'
True
Quote:
Quote:
>type(c).__name__ == 'instance'
True

On Sep 1, 2008, at 10:43 AM, Luca wrote:
Quote:
Hi all.
>
I think this is a newbie question... what is the best method to know
if a property of an object is a function?
>
I'm thinking something as
>
if type(obj.methodName)==???
>
Can someone help me?
>
--
-- luca
--
http://mail.python.org/mailman/listinfo/python-list
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (Darwin)

iD8DBQFIu606cZ70OCIgLecRAhE6AJ4r0GuHlWxXbLaYuolqpJ StYPD+ggCgidKg
qtgl+nbaKgH5AoelTu5WeJU=
=W4eG
-----END PGP SIGNATURE-----

Steven D'Aprano
Guest
 
Posts: n/a
#2: Sep 1 '08

re: Re: Know if a object member is a method


On Mon, 01 Sep 2008 10:52:10 +0200, Manuel Ebert wrote:

Quote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
>
Hi Luca,
>
use type(something).__name__ , e.g.
>
Quote:
Quote:
>>def x():
>>> pass
>>class C:
>>> pass
>>c = C()
>>type(x).__name__ == 'function'
True
Quote:
Quote:
>type(C).__name__ == 'classobj'
True
Quote:
Quote:
>type(c).__name__ == 'instance'
True

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:
Quote:
Quote:
Quote:
>>class function(object): # not a reserved name
.... pass
....
Quote:
Quote:
Quote:
>>x = function()
>>type(x).__name__
'function'
Quote:
Quote:
Quote:
>>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:
Quote:
Quote:
Quote:
>>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.



--
Steven
Luca
Guest
 
Posts: n/a
#3: Sep 1 '08

re: Re: Know if a object member is a method


On Mon, Sep 1, 2008 at 11:35 AM, Steven D'Aprano
<steven@remove.this.cybersource.com.auwrote:
Quote:
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:
>
Quote:
Quote:
>>>class function(object): # not a reserved name
... pass
...
Quote:
Quote:
>>>x = function()
>>>type(x).__name__
'function'
Quote:
Quote:
>>>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:
>
Quote:
Quote:
>>>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
Marc 'BlackJack' Rintsch
Guest
 
Posts: n/a
#4: Sep 1 '08

re: Re: Know if a object member is a method


On Mon, 01 Sep 2008 11:45:36 +0200, Luca wrote:
Quote:
Quote:
>But not so easy to fool a type check:
>>
Quote:
>>>>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"?
The "most pythonic" might be not checking at all but simply *call* the
object and deal with possible failures.

What's your use case?

Ciao,
Marc 'BlackJack' Rintsch
Steven D'Aprano
Guest
 
Posts: n/a
#5: Sep 1 '08

re: Re: Know if a object member is a method


On Mon, 01 Sep 2008 11:45:36 +0200, Luca asked about recognizing methods:
Quote:
What is the best way to do this? The "most pythonic"?
That depends on why you are doing it, and what you want to do with the
information once you've found it.

If you are experimenting in the interactive interpreter, the easiest way
is simply call the method and see what happens:

obj.methodName() # call the method

If it succeeds, then it is some sort of callable, a method or a function
or something more unusual.

If you fear side-effects, then use:

callable(obj.methodName)

Alternatively, you can read the docs:

help(obj)
help(obj.methodName)

If your aim is to write something like a debugger, profiler, or some
other application that needs to inspect arbitrary objects and work out
what they do, then you probably should be using:

isinstance(obj.methodName, new.instancemethod)

But remember that not all callable attributes are instancemethods!

There is no one right way of doing this.

Hope this helps.



--
Steven
Luca
Guest
 
Posts: n/a
#6: Sep 1 '08

re: Re: Know if a object member is a method


On Mon, Sep 1, 2008 at 2:18 PM, Steven D'Aprano
<steve@remove-this-cybersource.com.auwrote:
Quote:
>
If your aim is to write something like a debugger, profiler, or some
other application that needs to inspect arbitrary objects and work out
what they do, then you probably should be using:
>
isinstance(obj.methodName, new.instancemethod)
>
But remember that not all callable attributes are instancemethods!
>
There is no one right way of doing this.
>
Hope this helps.
Yes, this helps a lot. In facts I need to do something like a language parser.

Thanks all.

--
-- luca
Fredrik Lundh
Guest
 
Posts: n/a
#7: Sep 1 '08

re: Re: Know if a object member is a method


Luca wrote:
Quote:
Yes, this helps a lot. In facts I need to do something like a language parser.
a *parser* that works on object structures created by executing a Python
program?

</F>

Closed Thread