subclassing "file"

U

Uwe Mayer

Hi,

when extending a build in class, what does the constructor __init__(...)
have to return?
and how does the constructor call its base-class construtor? (or is this
done automatically?)

I want to derive from "file" to create a class that reads record from a
binary file:

class myFile(file):
def __init__(self, filename, mode="r", bufsize=-1):
....?...

just calling the basename and the constructor does not work:
<closed file '<uninitialized file>', mode '<uninitialized file>' at ...>

What am I missing?

Thanks for your comments
Ciao
Uwe
 
J

James Henderson

Hi,

when extending a build in class, what does the constructor __init__(...)
have to return?
and how does the constructor call its base-class construtor? (or is this
done automatically?)

I want to derive from "file" to create a class that reads record from a
binary file:

class myFile(file):
def __init__(self, filename, mode="r", bufsize=-1):
....?...

just calling the basename and the constructor does not work:

<closed file '<uninitialized file>', mode '<uninitialized file>' at ...>

What am I missing?

You don't show the second line of you __init__ method. It should be:

file.__init__(self, filename, mode, bufsize)

James
 
J

James Henderson

when extending a build in class, what does the constructor __init__(...)
have to return?

A bit more detail now I've read your message properly. :) It shouldn't have a
return statement. This means that it returns None.

Strictly speaking __init__ isn't a constructor, since the instance has already
been created before being passed to it. (A real constructor, __new__, has
been added to the language for subclassing immutable types.) __init__ just
modifies self in place.
and how does the constructor call its base-class construtor? (or is this
done automatically?)

The __init__ method of the base class, assuming it must be called at all, must
be called explicitly as an unbound method, as I showed in my last e-mail.
Reminder: if "self.__init__()" is the bound method then
"MyClass.__init__(self)" is the unbound method. The two are generally
equivalent, but the unbound form is needed in inheritance to specify a
superclass of self's immediate class.

J.
 
D

Dennis Lee Bieber

when extending a build in class, what does the constructor __init__(...)
have to return?

__init__() must not contain a "return xxx" statement. The
constructor returns the new object which has been passed to __init__()
as "self".
and how does the constructor call its base-class construtor? (or is this
done automatically?)
Unless things have changed, I believe it must be explicitly
invoked.
class myFile(file):
def __init__(self, filename, mode="r", bufsize=-1):
....?...

just calling the basename and the constructor does not work:

You really should have included the code you tried.
<closed file '<uninitialized file>', mode '<uninitialized file>' at ...>
Well, it looks like you properly created a file object, but it
has not been opened...

I couldn't find any examples in the standard library that did
inheritance from "file"; all the file-type things I saw worked by
overriding the file methods...

IE, something like:

class myFile:
def __init__(self, filename, mode...):
self.fil = file(filename, mode...) #or open(...)
for older releases
def close(self):
self.fil.close()
def read(self):
return self.fil.read()
def repr(self):
return repr(self.fil)
#etc.



<html>
<body>
<tt>--&nbsp; <br>
&nbsp;&gt; ============================================================== &lt;<br>
&nbsp;&gt;&nbsp;&nbsp; (e-mail address removed)&nbsp; | Wulfraed&nbsp; Dennis Lee Bieber&nbsp; KD6MOG &lt;<br>
&nbsp;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (e-mail address removed)&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Bestiaria Support Staff&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;<br>
&nbsp;&gt; ============================================================== &lt;<br>
&nbsp;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Home Page: &lt;<a href="http://www.dm.net/~wulfraed/" eudora="autourl">http://www.dm.net/~wulfraed/</a>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;<br>
&nbsp;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Overflow Page: &lt;<a href="http://wlfraed.home.netcom.com/" eudora="autourl">http://wlfraed.home.netcom.com/</a>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;<br>
</body>
</html>
 
P

Peter Otten

Uwe said:
Hi,

when extending a build in class, what does the constructor __init__(...)
have to return?
and how does the constructor call its base-class construtor? (or is this
done automatically?)

I want to derive from "file" to create a class that reads record from a
binary file:

class myFile(file):
def __init__(self, filename, mode="r", bufsize=-1):
....?...

just calling the basename and the constructor does not work:

<closed file '<uninitialized file>', mode '<uninitialized file>' at ...>

What am I missing?

Thanks for your comments
Ciao
Uwe

When you don't want to do anything in the constructor __init__(), it
suffices to override the methods of interest, e. g.:
.... def write(self, s):
.... file.write(self, s.upper())
....'A FOOLISH CONSCIOUSNESS'

Otherwise call base.__init__(self, someargs), e. g:
 
P

Peter Otten

Oops, hit the wrong button. The example goes like so:
.... def __init__(self, op, *args):
.... file.__init__(self, *args)
.... self.op = op
.... def write(self, s):
.... file.write(self, self.op(s))
....
Of course I used *args only because I'm to laze to name the actual arguments
one at a time :)

Peter
 
D

Dennis Lee Bieber

ACK!....

That's what I get for upgrading Eudora and cleaning up
signatures (since I share them with Agent).

Sorry about these...
<html>
<body>
<tt>--&nbsp; <br>
&nbsp;&gt; ============================================================== &lt;<br>
&nbsp;&gt;&nbsp;&nbsp; (e-mail address removed)&nbsp; | Wulfraed&nbsp; Dennis Lee Bieber&nbsp; KD6MOG &lt;<br>


Hope this set comes in clean.

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,947
Members
47,498
Latest member
yelene6679

Latest Threads

Top