total idiot question: +=, .=, etc...

Guido van Rossum guido at cnri.reston.va.us
Tue Jun 29 12:08:06 EDT 1999


> >>>     class A(B):
> >>>         def __init__(self, x, y, z):
> >>>             B.__init__(self, x, y, z)
> 
> [Greg Ewing]
> > I could live with having to explicitly name the
> > superclass, provided something checked that the
> > class I named was actually a direct base class
> > of the one where the method is defined. That
> > way things would be less likely to break
> > mysteriously when I rearrange the class
> > hierarchy.
> 
> [Guido]
> > Oh, but that check *is* being made!  (In fact this is the same check
> > that prevents "class methods" from working ;-)
> 
> Nope, that's a different check.  If C derives from B derives from A, this
> check allows either C.__init__ or B.__init__ to be called from A.__init__
> with an instance of A.  Greg wants  B.__init__ to be OK here, but not
> C.__init__ (C is not a direct base class of the one (A) where the method
> (A.__init__) is defined).  IOW, he wants a way to restrict __init__ calls to
> immediate bases of the class from which the __init__ is invoked, to e.g.
> prevent level-skipping under class rearrangement.

Sigh.  Indeed.

> I posted some ugly code to address that, but think it's really a job for a
> text editor and a pair of eyeballs <wink>.

Hm -- he may be right!  I was reading some IDLE code the other day and
noticed that PyShell's close() method was calling
PyShellEditorWindow.close(self) while it didn't inherit from that
class at all...  Turns out the actual method was defined in a common
base class -- a silent (and in this case harmless) bug, caused by,
indeed, a class restructuring.  So there is a point to his wish.  On
the other hand Python explicitly allows you to ignore base class
methods in overriding ones, and even constructors...  (And then again,
there is something to say for requiring constructors to play be
slightly different rules -- certainly they don't have to duplicate the
signature of the base class constructor like regular methods need to
do.)

Enough,

--Guido van Rossum (home page: http://www.python.org/~guido/)





More information about the Python-list mailing list