I got the following python code where printSaved is called when a button is clicked (using the wx library and python 2.7.2). But when this happens i got a really strange error
Traceback (most recent call last):
File "./program.py", line 135, in printSaved
s = self.readSaved()
TypeError: readSaved() takes no arguments (1 given)
Here is the code
def readSaved():
f = codecs.open((os.getenv('HOME') +'/Dokument/savefile.txt') ,'r','utf-8')
l = f.readlines()
f.close()
return l
def printSaved(self,event):
s = self.readSaved()
for l in s:
print l
I fixed the problem by adding one argument to readSaved(somethingUseless) and everything worked fine. My question is what is passed to readSaved and HOW/WHY? Does this have something to do with the event?
In Python, the object on which a method is called is always passed in explicitly as an argument. This argument is conventionally called
self.The correct definition for
readSaved()as an instance method should have this as the first line:If you want it to be a module-level function, you should call it as follows: