post init call
Ulrich Eckhardt
ulrich.eckhardt at dominolaser.com
Mon Jun 18 04:27:42 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)
Two things here:
1. You pass "color" as "shapename" to the baseclass' initialisation
function, which is a bit surprising.
2. You can use super(ColoredShape, self).__init__(color) or even
super(self).__init__(color).
> 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?
You might be able to, by hacking on the (meta?) class and how/when
things are constructed. I'm not sure how to approach that though.
What I would do is to use lazy initialisation, i.e. call update() when
it is actually needed. For that, you could create a decorator and put it
on all methods that require this initialisation.
Good luck!
Uli
More information about the Python-list
mailing list