post init call

P

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
 
U

Ulrich Eckhardt

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
 
T

Thomas Rachel

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
 
P

Peter Otten

Prashant said:
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?

I don't think so. You could however shuffle things around a bit:

class Shape(object):
def __init__(self, shapename, *args, **kw):
self.shapename = shapename
self.init(*args, **kw)
self.update()
def init(self, *args, **kw):
pass
def update(self):
print "update"

In that constellation you would override init(), not __init__():

class ColoredShape(Shape):
def init(self, color, **kw):
self.color = color
print 1
print 2
print 3
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top