Reference to self not passed to member function
Fredrik Lundh
fredrik at pythonware.com
Fri May 6 19:06:31 EDT 2005
James Stroud wrote:
> I did this:
>
> py> class bob(object):
> ... def __init__(self,**kwargs):
> ... for fname,func in kwargs.items():
> ... setattr(self, fname, lambda *args : func(*args))
> ...
> py> def doit():
> ... print "wuzzup?"
> ...
> py> abob = bob(doit=doit)
> py>
> py> abob.doit()
> wuzzup?
>
> Much to my surprise, this works fine.
why is that surprising? "doit" is a callable, you wrap it in another
callable, and then you store it in an attribute.
that's no different from storing a integer as an attribute, and it
sure doesn't turn your callable into an instance method.
> 1. What exactly is going on?
see above.
> 2. How can I get ref to self passed to doit() if I want it to?
try using this bob instead:
import new
class bob(object):
def __init__(self, **kwargs):
for fname, func in kwargs.items():
setattr(self, fname, new.instancemethod(func, self, bob))
</F>
More information about the Python-list
mailing list