[Tutor] OO newbie

briner work at infomaniak.ch
Thu Apr 7 21:21:43 CEST 2005


hi,

Hi, I'm having some trouble to understand how to program in OO and to
know what are the possibilities offered by OO programming in python.
This is even more difficult when you -unfortunately- don't know any
other OO languages.

for example I try to improve a dict class whom you can set up a default
value if the key doesn't exist and are able to count how many instance
it have

class SuperDict(dict):
   count = 0
   def __init__(self, *args, **kw):
      self.__class__.count = self.__class__.count+1
      if kw.has_key('default'):
         self.default=kw.pop('default')
      super(C,self).__init__( *args, **kw)
   def __getitem__(self, key):
      try:
         return dict.__getitem__(self, key)
      except KeyError:
         return self.default
   def __del__(self):
      self.__class__.count = self.__class__.count - 1
   def getCount(cls):
      return cls.count

question:

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??

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

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

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

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

4-
I just realize when writing this email that SuperDict is also an object.
and 
type(SuperDict)
<type 'type'>
WOW

Cedric BRINER



More information about the Tutor mailing list