[Tutor] Question about lambda and new.instancemethod
Tino Dai
tinoloc at gmail.com
Wed Jul 11 00:51:30 CEST 2007
Hi Everybody,
I have been working on a parser for myself. I want to create methods on
the fly with the information that I get from a file. Here is my question:
class configure:
<.....stuff deleted......>
def parseGlobal(self,projectName,section,project):
for element in section.childNodes:
if not element.nodeName == "#text":
self.glblFunc["get" +
project.getAttribute("name").encode("ascii").capitalize()
+ element.nodeName.encode("ascii").capitalize()] = \
lambda self : element.firstChild.data.encode("ascii")
<...and later on...>
def addMethod(instName,funcDict):
for k,v in funcDict.iteritems():
instName.__dict__[k]=new.instancemethod(v,instName,'configure')
The instance name and funcDict (which is the populated self.glblFunc) get
sent up to addMethod. This is supposed to create a new method within the
instance of my class. Instead, I get a AttributeError exception thrown. So,
I check the code using inspect.getsource, and for all of the new methods
that I create, I get
' + element.nodeName.encode("ascii").capitalize()] = lambda
self : sys.stdout.write(element.firstChild.data.encode("ascii"))\n\n'
instead of
lambda self : sys.stdout.write(element.firstChild.data.encode("ascii")
Could anybody tell me why this is happening or do you need some more code
Thanks,
Tino
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070710/50a5c192/attachment.htm
More information about the Tutor
mailing list