can a method access/set another's variables?

7stud bbxx789_05ss at yahoo.com
Mon Apr 2 00:03:34 EDT 2007


On Apr 1, 7:56 pm, "wswilson" <wswil... at gmail.com> wrote:
> I am parsing a document which contains some lines with code I want to
> eval or exec. However, due to the complexity of the parsing, I am
> splitting it among different methods. So, if I eval something in one
> method, it won't be there if I try to access its value a few lines
> later since I happen to be in a different method in the parser. Thanks
> for the help!

val = None
class A(object):
        def early_parse(self):
                global val
                self.result1 = val = eval("10 + 5")
def later_parse():
            global val
            val += eval("20*2")

a = A()
a.early_parse()
later_parse()
a.result2 = val
print a.result1
print a.result2




More information about the Python-list mailing list