Gregor Lingl wrote: > Hi everyoen, > > 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? > Are there pros or cons for one of both > variants?
They are different. Turtle = Pen creates an alias - another name that refers to the same object. Changes made through either name will affect both. This is similar to what happens if you give two names to a list, for example. In [1]: class Pen(object): ...: value = 3 ...: def show(self): ...: print type(self), self.value ...: ...: In [2]: Turtle = Pen In [3]: p = Pen() In [4]: p.show() <class '__main__.Pen'> 3 In [5]: t = Turtle() In [6]: t.show() <class '__main__.Pen'> 3 In [7]: Turtle.value = 'new value' In [8]: p.show() <class '__main__.Pen'> new value In [9]: type(p) == type(t) Out[9]: True Creating a subclass makes a new class that inherits all the behaviour of the original class. However you may modify the subclass after it is created and the modifications won't affect the base class, and objects of the new class have a different type than objects of the base class: In [10]: class Turtle(Pen): pass ....: In [11]: t=Turtle() In [12]: t.show() <class '__main__.Turtle'> new value In [13]: Turtle.value = 'turtle' In [14]: t.show() <class '__main__.Turtle'> turtle In [15]: p.show() <class '__main__.Pen'> new value If you want an alias, use an alias. If you need a subclass, make a subclass. They're not the same! Kent _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor