Obtaining an member function by name

G

guy lateur

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
 
D

Diez B. Roggisch

guy said:
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
 
B

Bengt Richter

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.,
... def bar():
...
File "<stdin>", line 3

^
IndentationError: expected an indented block ... def bar(): return 'bar is the name' # you could have done this
... 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) Traceback (most recent call last):
... def bar(self): return self, 'bar is the name' # you could have done this
... 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) (<__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
 
E

EuGeNe

guy said:
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?
@staticmethod
def bar():
pass

 
B

Bengt Richter

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.
... def bar(self): return self, 'bar is the name'
... ... # in the globals() directory, which we can access with appended ['foo']
...
>>> globals()['foo']
>>> #if you want the 'bar' attribute using the string
... getattr(globals()['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
 
G

guy lateur

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top