Prevent self being passed to a function stored as a member variable?
Jean-Paul Calderone
exarkun at divmod.com
Mon Sep 4 12:53:09 EDT 2006
On 4 Sep 2006 09:39:32 -0700, Sandra-24 <sandravandale at yahoo.com> wrote:
>How can you prevent self from being passed to a function stored as a
>member variable?
This doesn't happen:
>>> class x:
... def __init__(self):
... self.x = lambda: None
...
>>> x().x()
>>> class y(object):
... def __init__(self):
... self.y = lambda: None
...
>>> y().y()
>>>
Perhaps you confused this case with the case of a function which is a
class attribute?
>>> z().z()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: <lambda>() takes no arguments (1 given)
>>>
In which case, staticmethod() is actually the solution you want:
>>> class w(object):
... w = staticmethod(lambda: None)
...
>>> w().w()
>>>
Jean-Paul
More information about the Python-list
mailing list