[Tutor] Alias for a class name
Alan Gauld
alan.gauld at freenet.co.uk
Thu Jul 13 08:50:59 CEST 2006
> say in some module I have defined a class Pen
> and I want an alias for the class name, say
> Turtle
>
> I can do this:
>
> class Turtle(Pen):
> pass
>
> or simply (after having defined Pen):
>
> Turtle = Pen
>
> Are those two variants different in effect?
Yes, the first creates a new class which is a subclass of Pen.
When you cxall a method on an instance Pyhon will go
through the usual lookup routine of searching Turtle first
then looking in Pen.
The second is just an alias of Pen so an instance of Turtle
is an instance of Pen. Method invocation looks in Pemn
immediately.
> Are there pros or cons for one of both
> variants?
I can't think of any real advantage to the first one and
its slightly slower. If you want an alias use the second method.
OTOH If you might want to subclass Turtle then the first
might be better - although I can't think of a really good
reason why.... It just reflects the intention better.
HTH,
Alan G.
More information about the Tutor
mailing list