I have defined a class to process a file but get the following error when I try to instantiate the class and pass the filename. Let me know what would be the problem?
>>> class fileprocess:
... def pread(self,filename):
... print filename
... f = open(filename,'w')
... print f>>> x = fileprocess>>> x.pread('c:/test.txt')
Traceback (most recent call last):
File "", line 1, in
TypeError: unbound method pread() must be called with
fileprocess instance as first argument (got nothing instead)
3 Answers 3
x = fileprocess does not mean x is an instance of fileprocess. It means x is now an alias for the fileprocess class.
You need to create an instance, using the ().
x = fileprocess()
x.pread('c:/test.txt')
In addition, based on your original code, you could use x to create class instances.
x = fileprocess
f = x() # creates a fileprocess
f.pread('c:/test.txt')
Comments
x = fileprocess should be x = fileprocess()
Currently x is referring to the class itself, not an instance of the class. So when you call x.pread('c:/test.txt') that's the same as calling fileprocess.pread('c:/test.txt')
Comments
But why use write mode for read function? Maybe it's pwrite?