Re: Know if a object member is a method

2008-09-01 Thread Manuel Ebert

-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Hi Luca,

use type(something).__name__ , e.g.

 def x():
pass
 class C:
pass
 c = C()
 type(x).__name__ == 'function'
True
 type(C).__name__ == 'classobj'
True
 type(c).__name__ == 'instance'
True

On Sep 1, 2008, at 10:43 AM, Luca wrote:


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)

iD8DBQFIu606cZ70OCIgLecRAhE6AJ4r0GuHlWxXbLaYuolqpJStYPD+ggCgidKg
qtgl+nbaKgH5AoelTu5WeJU=
=W4eG
-END PGP SIGNATURE-
--
http://mail.python.org/mailman/listinfo/python-list


Re: Know if a object member is a method

2008-09-01 Thread Steven D'Aprano
On Mon, 01 Sep 2008 10:43:25 +0200, Luca wrote:

 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?

That's not quite as easy as you might think. In my testing, calling 
type(obj.methodName) can give any of the following:

type 'instancemethod'
type 'function'
type 'builtin_function_or_method'


There may be others as well.

Possibly all you really need is to check if the attribute is callable 
without caring what type of method or function it is:

 callable(obj.methodName)
True

In my opinion, that's the best way.


If you are sure that the method won't have side-effects or bugs:

 try:
... obj.method()
... except:
... print Not a method
...

(But how can you be sure?)


If you insist on an explicit test, try this:

import new
type(obj.methodName) == new.instancemethod


or alternatively:

 isinstance(obj.methodName, new.instancemethod)
True




-- 
Steven
--
http://mail.python.org/mailman/listinfo/python-list


Re: Know if a object member is a method

2008-09-01 Thread Steven D'Aprano
On Mon, 01 Sep 2008 10:52:10 +0200, Manuel Ebert wrote:


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

 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.



-- 
Steven
--
http://mail.python.org/mailman/listinfo/python-list


Re: Know if a object member is a method

2008-09-01 Thread Luca
On Mon, Sep 1, 2008 at 11:35 AM, Steven D'Aprano
[EMAIL PROTECTED] 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
--
http://mail.python.org/mailman/listinfo/python-list


Re: Know if a object member is a method

2008-09-01 Thread Marc 'BlackJack' Rintsch
On Mon, 01 Sep 2008 11:45:36 +0200, Luca wrote:

 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?

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
--
http://mail.python.org/mailman/listinfo/python-list


Re: Know if a object member is a method

2008-09-01 Thread Tino Wildenhain

Luca wrote:

On Mon, Sep 1, 2008 at 11:35 AM, Steven D'Aprano
[EMAIL PROTECTED] wrote:

...

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?


The question would be - why do you want this? Is it just
curiosity or can we find a better solution to the overall
problem, once understood?

Regards
Tino


smime.p7s
Description: S/MIME Cryptographic Signature
--
http://mail.python.org/mailman/listinfo/python-list

Re: Know if a object member is a method

2008-09-01 Thread Steven D'Aprano
On Mon, 01 Sep 2008 11:45:36 +0200, Luca asked about recognizing methods:

 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
--
http://mail.python.org/mailman/listinfo/python-list


Re: Know if a object member is a method

2008-09-01 Thread Luca
On Mon, Sep 1, 2008 at 2:18 PM, Steven D'Aprano
[EMAIL PROTECTED] wrote:

 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
--
http://mail.python.org/mailman/listinfo/python-list


Re: Know if a object member is a method

2008-09-01 Thread Fredrik Lundh

Luca wrote:


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

--
http://mail.python.org/mailman/listinfo/python-list