[Tutor] OO newbie

Alan Gauld alan.gauld at freenet.co.uk
Thu Apr 7 23:24:04 CEST 2005


> 1-
> this exemple will also works if you replace the:
> super(C,self).__init__( *args, **kw)
> by
> dict.__init__(self, *args, **kw)
>
> but I do not understand this dict.__init_... call.
> Shouldn't you call the super class constructor??

super is just a convenience feature added to make Python slightly
more like some other OOP languages. It is effectively just a
wrapper around the explicit call to the super class:

Thus super(C,self...) is the same as

dict.__init__(self...)

The second form just explicitly calls the __init__() method
of the dict class.

> and how can you distinguish the constructor of one superClass to
another
> in case of polymorhisme
> eg:
> class A:
>    def __init__(self,fooValue):
>       self.fooValue=fooValue

Thats where super breaks down IMHO and its much easier using the
explicit form. Thus

> and redefine
> class SuperDict(dict,A):
> so now how do you do to call the constructor of A

     def __init__(self,foo):
        dict.__init__(self)
        A.__init__(self.foo)
        # any local initialising here

> 2-
> what are the differences between
> self.__DoubleUnderscore

magic method name used by Python - operator names, init etc


> self._SimpleUnderscore

A name you want to hide from the outside world ('private'
in some other OOP languages)

> 3-
> in the definition of getCount we saw that the parameter is `cls'.
> what does that mean???

Its just a convention like using self for instance methods.
getcount() is a class method because count is a class variable.
Thus using cls instead of self kind of highlights that. In fact
there's no real diffrence in this case.

> I just realize when writing this email that SuperDict is also an
object.

All classes in Python are also objects. This is also the case
in several other OOP languages but by no means all. And indeed
WOW!, it is a very powerful feature. :-)

> type(SuperDict)
> <type 'type'>
> WOW

HTH,

Alan G.



More information about the Tutor mailing list