Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Obtaining an member function by name

Reply
Thread Tools

Obtaining an member function by name

 
 
guy lateur
Guest
Posts: n/a
 
      11-19-2005
Hi all,

Suppose you have this class:

class foo:
def bar():

Suppose you also have the strings "foo" and "bar". How can you obtain the
function foo.bar()?

Surely somebody knows..

TIA,
g


 
Reply With Quote
 
 
 
 
bonono@gmail.com
Guest
Posts: n/a
 
      11-19-2005
f = getattr(obj,"bar")
f()

guy lateur wrote:
> Hi all,
>
> Suppose you have this class:
>
> class foo:
> def bar():
>
> Suppose you also have the strings "foo" and "bar". How can you obtain the
> function foo.bar()?
>
> Surely somebody knows..
>
> TIA,
> g


 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      11-19-2005
guy lateur wrote:
> Hi all,
>
> Suppose you have this class:
>
> class foo:
> def bar():
>
> Suppose you also have the strings "foo" and "bar". How can you obtain the
> function foo.bar()?
>
> Surely somebody knows..


getattr helps. However, your example won't work: it misses either a
staticmethod-declaration, or a self-argument, or a classmethod and
cls-argument. So unless we know if bar shall be an instance.method or
not, it's hard to tell what exactly you want. Because you could want

getattr(getattr(mymodule, "foo"), "bar")

Or

getattr(getattr(mymodule, "foo")(), "bar")

(notice the parentheses)

or

getattr(getattr(locals(), "foo"), "bar")

or

getattr(getattr(globals(), "foo"), "bar")

Diez
 
Reply With Quote
 
Bengt Richter
Guest
Posts: n/a
 
      11-19-2005
On Sat, 19 Nov 2005 14:12:25 GMT, "guy lateur" <> wrote:

>Hi all,
>
>Suppose you have this class:
>
>class foo:
> def bar():
>
>Suppose you also have the strings "foo" and "bar". How can you obtain the
>function foo.bar()?

Why don't you type these things into an interactive python session
and see what happens? Also, foo.bar will be an unbound method of foo,
not a function per se. You could experiment a little, e.g.,

>>> class foo:

... def bar():
...
File "<stdin>", line 3

^
IndentationError: expected an indented block
>>> class foo:

... def bar(): return 'bar is the name' # you could have done this
...
>>> foo.bar()

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unbound method bar() must be called with foo instance as first argument (got nothing
instead)
>>> foo()

<__main__.foo instance at 0x02EF3D8C>
>>> foo.bar(foo())

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: bar() takes no arguments (1 given)
>>> class foo:

... def bar(self): return self, 'bar is the name' # you could have done this
...
>>> fooinst = foo()
>>> fooinst

<__main__.foo instance at 0x02EF756C>
>>> foo.bar(fooinst)

(<__main__.foo instance at 0x02EF756C>, 'bar is the name')
>>> fooinst.bar

<bound method foo.bar of <__main__.foo instance at 0x02EF756C>>
>>> fooinst.bar()

(<__main__.foo instance at 0x02EF756C>, 'bar is the name')
>>> foo.bar.im_func

<function bar at 0x02EF5764>
>>> foo.bar.im_func('first arg')

('first arg', 'bar is the name')
>>> fooinst.bar

<bound method foo.bar of <__main__.foo instance at 0x02EF756C>>
>>> fooinst.bar.im_func

<function bar at 0x02EF5764>
>>> fooinst.bar.im_func(1234)

(1234, 'bar is the name')
>>> fooinst.bar()

(<__main__.foo instance at 0x02EF756C>, 'bar is the name')
>>> foo.bar(333)

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unbound method bar() must be called with foo instance as first argument (got int inst
ance instead)
>>> foo.bar(fooinst)

(<__main__.foo instance at 0x02EF756C>, 'bar is the name')

Someone can explain. If you do some of your own work, it will help even the load.
Have you looked at any documentation? Start at http://www.python.org/
and click a few things. There seems to be a beginners guide link under documentation
in the sidebar to the left

Regards,
Bengt Richter
 
Reply With Quote
 
EuGeNe
Guest
Posts: n/a
 
      11-19-2005
guy lateur wrote:
> Hi all,
>
> Suppose you have this class:
>
> class foo:
> def bar():
>
> Suppose you also have the strings "foo" and "bar". How can you obtain the
> function foo.bar()?
>
> Surely somebody knows..
>
> TIA,
> g
>
>



Would that do?

>>> class foo:

@staticmethod
def bar():
pass


>>> foo.bar

<function bar at 0x00B445F0>
>>>

 
Reply With Quote
 
Bengt Richter
Guest
Posts: n/a
 
      11-19-2005
On Sat, 19 Nov 2005 14:12:25 GMT, "guy lateur" <> wrote:

>Hi all,
>
>Suppose you have this class:
>
>class foo:
> def bar():
>
>Suppose you also have the strings "foo" and "bar". How can you obtain the
>function foo.bar()?
>
>Surely somebody knows..
>

Sorry, clean forgot about the strings.

>>> class foo:

... def bar(self): return self, 'bar is the name'
...
>>> # The definition is at global scope, so 'foo' will show up

... # in the globals() directory, which we can access with appended ['foo']
...
>>> globals()['foo']

<class __main__.foo at 0x02EE9C2C>
>>> #if you want the 'bar' attribute using the string

... getattr(globals()['foo'], 'bar')
<unbound method foo.bar>
>>> foo.bar

<unbound method foo.bar>

Note that getting an attribute does some "binding" magic if the
attribute has certain qualitites. In this case the bar function
is associated with the foo class to become an "unbound method"

Nit: usual convention is to spell class names with leading upper case.
Then you can e.g. use the lower case same name for an instance of the
class without confusions. Nit2: using new-style classes, which derive
from object (or also other bases, but at least object or type) is now
recommended, so you get the full-fledged attribute machinery that supports
much of the latest magic. So write the above more like

class Foo(object):
def bar(self): return self, 'bar is the name'

Regards,
Bengt Richter
 
Reply With Quote
 
guy lateur
Guest
Posts: n/a
 
      11-19-2005
Thanks for the feedback, people.

I actually only need the "bar" part (instance methods). I added the "foo"
part to generalize the question without really thinking it through first.
Still, it has gotten me more information than I ever imagined. So thanks
again.

g


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Obtaining member function/function object's operator() signature psujkov@gmail.com C++ 1 02-14-2007 12:59 PM
performance of static member function vs. instance member function 0to60 C++ 4 11-21-2003 05:25 PM
Function pointer member variable to non-member function Alex C++ 0 10-15-2003 05:26 PM
Function pointer member variable to non-member function slide_o_mix C++ 0 10-15-2003 03:37 PM
Passing a pointer to member function as a parameter to another member function Newsgroup - Ann C++ 5 07-30-2003 02:54 AM



Advertisments