From: Samuele P. <ped...@bl...> - 2002-02-04 22:42:11
|
Hi. > Hi All, > > I intended to have a method in a class that have an overload method. I > have the following code in Test.py: > > class Test: > def __init__(self): > self.autoCommit = 1; > def Commit(self, commit=self.autoCommit): > self.autoCommit = commit; > > When executing it in Jython, I got the following error: > > >>> execfile("Test.py") > Traceback (innermost last): > File "<console>", line 1, in ? > File "Test.py", line 1, in ? > File "Test.py", line 4, in Test > NameError: self > > Basically it complain about the Commit method signature. What's the > correct way of doing this in Jython? Thanks. > The problem you're seeing is because default argument expressions are evaluated at function definition time not each time the function is invoked, so the reference to self in commit= does not make sense. A possible working rewrite is: def Commit(self, commit=None): if commit is None: commit = self.autoCommit self.autoCommit = commit Btw you don't need to terminate statements with ';' It is used to separate simple statements on a same line, not a very usual practice in Python. regards, Samuele Pedroni |