Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Obtaining an member function by name (http://www.velocityreviews.com/forums/t351688-obtaining-an-member-function-by-name.html)

guy lateur 11-19-2005 02:12 PM

Obtaining an member function by name
 
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



bonono@gmail.com 11-19-2005 02:29 PM

Re: Obtaining an member function by name
 
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



Diez B. Roggisch 11-19-2005 02:36 PM

Re: Obtaining an member function by name
 
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

Bengt Richter 11-19-2005 03:30 PM

Re: Obtaining an member function by name
 
On Sat, 19 Nov 2005 14:12:25 GMT, "guy lateur" <guy.lateurNNOOSSPPAAMM@pandora.be> 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

EuGeNe 11-19-2005 03:42 PM

Re: Obtaining an member function by name
 
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>
>>>


Bengt Richter 11-19-2005 03:48 PM

Re: Obtaining an member function by name
 
On Sat, 19 Nov 2005 14:12:25 GMT, "guy lateur" <guy.lateurNNOOSSPPAAMM@pandora.be> 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

guy lateur 11-19-2005 04:28 PM

Re: Obtaining an member function by name
 
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




All times are GMT. The time now is 03:21 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57