1

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)
asked Nov 16, 2011 at 22:17

3 Answers 3

7

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')
answered Nov 16, 2011 at 22:20
Sign up to request clarification or add additional context in comments.

Comments

3

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')

answered Nov 16, 2011 at 22:19

Comments

0

But why use write mode for read function? Maybe it's pwrite?

answered Nov 17, 2011 at 1:19

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.