inheriting file object

Jeremy Jones zanesdad at bellsouth.net
Wed Jul 6 13:25:06 EDT 2005


Jeremy wrote:

>Hello all,
>	I am trying to inherit the file object and don't know how to do it.  I 
>need to open a file and perform operations on it in the class I am 
>writing.  I know the simple syntax is:
>
>class MyClass(file):
>	...
>
>but I don't know how to make it open the file for reading/writing.  Can 
>anyone help me out with this?
>Thanks,
>Jeremy
>
>  
>
Something like this?  I put the following code in test_file.py:

class MyFile(file):
    def doing_something(self):
        print "in my own method"


And used it like this:

In [1]: import test_file

In [2]: f = test_file.MyFile("foobar.file", "w")

In [3]: f.write("foo\n")

In [4]: f.doing_something()
in my own method


But do you really need to subclass file, or can you just use a file 
instance in your class?


Jeremy Jones  



More information about the Python-list mailing list