2008-06-08
Thinking about Python's inheritance model
I recently read an entry by someone who
was surprised that Python subclasses did not automatically call their
parent class's 'constructor' method, by which he meant __init__
.
This is a straightforward consequence of Python's inheritance model,
but Python's inheritance model is slightly odd.
The straightforward way to explain Python's inheritance model is that it uses what I could call 'lookup based' or 'name based' subclassing, where what subclassing does is change what binding will be returned when you look up a given name on an instance of the (sub)class. And that's pretty much it as for what subclassing does.
(Technically subclassing has some additional effects when you use
__slots__
, but that's another discussion.)
The next thing to know is that all of Python's special operations,
including __init__
, work by normal method calls; they look up the
binding for the name of the special method and then call what they get
back (if anything). So in this model it makes perfect sense that there
is no upcall to the parent class's __init__
method; your subclass
overrode the binding of the name, so your method function gets called
instead of the parent's. Doing an upcall would make __init__
a magic
special operation, unlike all of the others.
(Note that multiple inheritance does not preclude magic upcalls, since there is a defined method resolution order that the upcalling could follow.)
One consequence of this simple model of subclassing is that you can
fake subclassed instances by using other methods
and outside people generally won't notice anything. (They can tell
if they use isinstance()
or the like, but most code shouldn't.)
Recovering my Eee PC from a post-update problem
I recently applied a bunch of pending Eee updates from Asus, and suddenly the full desktop mode that I prefer stopped working. The Eee always booted into the basic interface, and when I switched back to advanced mode everything in my Desktop directory had disappeared (which is bad, because I have some customized launchers there)
(Fortunately I had an off-machine backup of my Desktop directory so I could restore my customizations.)
I enabled advanced desktop mode the easy way, namely by installing the
advanced-desktop-eeepc
package. After poking around and trying several
things, what fixed the problem problem was to apt-get remove
and then
apt-get install
the package again.
(I assume that something in one of the Asus updates overwrote a customization that the advanced desktop stuff required, although I couldn't spot anything obvious. It is a little disturbing to me that something was apparently deleting all additions to my Desktop folder, although I don't know whether this is something in the basic desktop or in the advanced desktop setup stuff.)
Sidebar: the launchers I've found useful
By 'launchers' I mean icons on the desktop that start various
programs when clicked, which are created by .desktop
files in your
$HOME/Desktop
directory. The ones I use are: blank the screen, start konsole
, start xterm
, and start
a black-on-white xterm
(instead of the default white on black colour scheme).