By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,835 Members | 1,346 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,835 IT Pros & Developers. It's quick & easy.

subclassing "file"

P: n/a
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:
f = myFile("testfile")
f

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

What am I missing?

Thanks for your comments
Ciao
Uwe
Jul 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Mon, 19 Jan 2004 17:14:33 +0100, Uwe Mayer <me*****@hadiko.de>
declaimed the following in comp.lang.python:
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.
f = myFile("testfile")
f

<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; wl*****@ix.netcom.com&nbsp; | Wulfraed&nbsp; Dennis Lee Bieber&nbsp; KD6MOG &lt;<br>
&nbsp;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; wu******@dm.net&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;&nbs p;&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;&n bsp;&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;&n bsp; &lt;<br>
</body>
</html>
Jul 18 '05 #2

P: n/a
Uwe Mayer wrote:
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:
f = myFile("testfile")
f <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.:
class myfile(file): .... def write(self, s):
.... file.write(self, s.upper())
.... f = myfile("tmp.txt", "w")
f.write("a foolish consciousness")
f.close()
file("tmp.txt").read()

'A FOOLISH CONSCIOUSNESS'

Otherwise call base.__init__(self, someargs), e. g:
Jul 18 '05 #3

P: n/a
On Monday 19 January 2004 4:14 pm, Uwe Mayer wrote:
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:
f = myFile("testfile")
f


<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
--
James Henderson, Logical Progression Ltd.
http://www.logicalprogression.net/
http://sourceforge.net/projects/mailmanager/
Jul 18 '05 #4

P: n/a
Oops, hit the wrong button. The example goes like so:
class myfile(file): .... def __init__(self, op, *args):
.... file.__init__(self, *args)
.... self.op = op
.... def write(self, s):
.... file.write(self, self.op(s))
.... f = myfile(lambda s: "-".join(s), "tmp.txt", "w")
f.write("a foolish consciousness")
f.close()
file("tmp.txt").read() 'a- -f-o-o-l-i-s-h- -c-o-n-s-c-i-o-u-s-n-e-s-s'


Of course I used *args only because I'm to laze to name the actual arguments
one at a time :-)

Peter
Jul 18 '05 #5

P: n/a
On Mon, 19 Jan 2004 16:57:11 GMT, Dennis Lee Bieber
<wl*****@ix.netcom.com> declaimed the following in comp.lang.python:
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; wl*****@ix.netcom.com&nbsp; | Wulfraed&nbsp; Dennis Lee Bieber&nbsp; KD6MOG &lt;<br>

Hope this set comes in clean.

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #6

P: n/a
On Monday 19 January 2004 4:14 pm, Uwe Mayer wrote:
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.
--
James Henderson, Logical Progression Ltd.
http://www.logicalprogression.net/
http://sourceforge.net/projects/mailmanager/
Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.