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
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to