how to get name of function from within function?
Andrew Dalke
dalke at dalkescientific.com
Sat Jun 4 02:49:26 EDT 2005
I'm with Steven Bethard on this; I don't know what you
(Christopher J. Bottaro) are trying to do.
Based on your example, does the following meet your needs?
>>> class Spam(object):
... def funcA(self):
... print "A is called"
... def __getattr__(self, name):
... if name.startswith("_"):
... raise AttributeError, name
... f = get_function(name)
... if f is not None:
... return f
... raise AttributeError, name
...
>>> def get_function(name):
... return globals().get(name + "IMPL", None)
...
>>> x = Spam()
>>> x.funcA()
A is called
>>> x.funcB()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 10, in __getattr__
AttributeError: funcB
>>> def funcBIMPL():
... print "Calling all bees"
...
>>> x.funcB()
Calling all bees
>>>
Confused-ly-your's
Andrew
dalke at dalkescientific.com
More information about the Python-list
mailing list