Reference to self not passed to member function

J

James Stroud

Hello All,

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.

My questions:

1. What exactly is going on?
2. How can I get ref to self passed to doit() if I want it to? This:

abob.doit(abob)

seems like the hard way

Any ideas?

James


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
 
W

wittempj

I think it is more clear to rephrase your code as:
-#!/usr/bin/env python
-class bob(object):
- def __init__(self,**kwargs):
- print kwargs
- for fname,func in kwargs.items():
- setattr(self, fname, lambda *args : func(*args))
-
-def doit():
- print "wuzzup?"
-
-
-abob = bob(sayyoudo=doit)
-
-abob.sayyoudo()
outpu is now:
martin@ubuntu:~$ ./test.py
{'sayyoudo': <function doit at 0xb7dfcdf4>}
wuzzup?
martin@ubuntu:~$

so property sayyoudo points to method doit
 
S

Scott David Daniels

James said:
Hello All,

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.
> 1. What exactly is going on?

This behavior shouldn't surprise you. You stored a function as
an attribute. In fact you could have simply done:
py> class bob(object):
.... def __init__(self,**kwargs):
.... for fname, function in kwargs.items():
.... setattr(self, fname, function)
2. How can I get ref to self passed to doit() if I want it to? This:
abob.doit(abob)

py> import new
py> class carol(object):
.... def __init__(self, **kwargs):
.... for name, method in kwargs.items():
.... setattr(self, name,
.... new.instancemethod(method, self, carol))

This should behave as you prefer.


--Scott David Daniels
(e-mail address removed)
 
J

James Stroud

Thanks to both Scott and Fredrik.

James

This behavior shouldn't surprise you. You stored a function as
an attribute. In fact you could have simply done:
py> class bob(object):
... def __init__(self,**kwargs):
... for fname, function in kwargs.items():
... setattr(self, fname, function)


py> import new
py> class carol(object):
... def __init__(self, **kwargs):
... for name, method in kwargs.items():
... setattr(self, name,
... new.instancemethod(method, self, carol))

This should behave as you prefer.


--Scott David Daniels
(e-mail address removed)

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top