Subclassing file and getting around the file.__init__ rigidity

"Martin v. Löwis" martin at v.loewis.de
Fri Apr 2 01:57:20 EST 2004


Jan Burgy wrote:
> Consider the class down below. I've implemented it just because I
> needed the pushback method. Now of course the third line in __init__
> doesn't work and I even understand why. My question is: is there any
> way to make it work?

What do you mean, it doesn't work? It does precisely what it says it
does:

> class BufferedFile(file):
> 
>     def __init__(self, name):
>         if type(name) == file:
>             self = name                   # DOESN'T WORK!

This assigns the local variable self to be the value of the local
variable name. As a result of that assignment, self does not refer
anymore to the object being created, but instead, it refers to
the first parameter. The object just being created is then not
accessible anymore.

If you thought this assignment might modify the object being created
in some way: Why would you think so?

Regards,
Martin




More information about the Python-list mailing list