post init call

Thomas Rachel nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de
Mon Jun 18 06:21:59 EDT 2012


Am 18.06.2012 09:10 schrieb Prashant:
> class Shape(object):
>      def __init__(self, shapename):
>          self.shapename = shapename
>      def update(self):
>          print "update"
>
> class ColoredShape(Shape):
>      def __init__(self, color):
>          Shape.__init__(self, color)
>          self.color = color
>          print 1
>          print 2
>          print 3
>          self.update()
>
> User can sub-class 'Shape' and create custom shapes. How ever user must call 'self.update()' as the last argument when ever he is sub-classing 'Shape'.
> I would like to know if it's possible to call 'self.update()' automatically after the __init__ of sub-class is done?
>
> Cheers

I would construct it this way:

class Shape(object):
     def __init__(self, *args):
         self.args = args
         self.init()
         self.update()
         # or: if self.init(): self.update()
     def init(self):
         return False # don't call update
     def update(self):
         print "update"
     @propery
     def shapename(self): return self.args[0]

class ColoredShape(Shape):
      def init(self):
          print 1, 2, 3
          self.update()
          return True
      @property
      def color(self): return self.args[1]


Thomas



More information about the Python-list mailing list