473,322 Members | 1,307 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

inheritance problem with 2 cooperative methods

Here is a problem I am having trouble with and I hope someone in this group
will suggest a solution. First, some code that works. 3 classes that are
derived from each other (A->B->C), each one implementing only 2 methods,
__init__ and setConfig.
-------------------------------------------------------
#!/usr/bin/python
class A (object):
def __init__(self):
super(A, self).__init__()
self.x = 0
def setConfig(self, config):
self.x += config['x']

class B (A):
def __init__(self):
super(B, self).__init__()
self.y = 0
def setConfig(self, config):
super(B, self).setConfig(config)
self.y += config['y']

class C (B):
def __init__(self):
super(C, self).__init__()
self.z = 0
def setConfig(self, config):
super(C, self).setConfig(config)
self.z += config['z']

config = {'x':1, 'y':2, 'z':3}
alpha = A()
alpha.setConfig(config)
print alpha.x
beta = B()
beta.setConfig(config)
print beta.x, beta.y
beta.setConfig(config)
print beta.x, beta.y
gamma = C()
gamma.setConfig(config)
print gamma.x, gamma.y, gamma.z
gamma.setConfig(config)
print gamma.x, gamma.y, gamma.z
--------------------------------------------------

The output from that code is:
1
1 2
2 4
1 2 3
2 4 6

So far, so good! But let's assume that I want to change the __init__
methods so that they take a configuration as an argument so the objects are
created and configured in one step, like this:
alpha = A(config)

How can the code be changed to implement this? It is tempting to modify the
__init__ methods like this:
class A (object):
def __init__(self, config):
super(A, self).__init__(config)
self.x = 0
A.setConfig(self, config)

However, if implemented this way, the output is:
1
2 2
3 4
3 4 3
4 6 6

This shows that setConfig gets called more than once because both __init__
and setConfig are cooperative methods.

I have been thinking about this for a day now and I cannot find a good
solution. I imagine this would be a common problem and that there would be
a recipe somewhere, but I couldn't find one (I looked in the Python
Cookbook). I've thought of creating 2 separate methods instead of
setConfig, one that does the "local" configuration and another one that
takes care of invoking super. But I don't like the idea of creating 2
methods in each class and I haven't been able to think of a way to solve the
problem with just one change in the base class that would be inherited by
all the subclasses.

Anyone has any ideas? Thanks.

Dan
Jul 18 '05 #1
8 1520
Dan Perl wrote:
So far, so good! But let's assume that I want to change the __init__
methods so that they take a configuration as an argument so the objects are
created and configured in one step, like this:
alpha = A(config)


One way would be to make the setConfig call only
in the root class, and perform the initialisation
that it depends on *before* making the super call
in each __init__ method, i.e.

class A (object):
def __init__(self, config):
self.x = 0
self.setConfig(config)

class B (A):
def __init__(self, config):
self.y = 0
super(B, self).__init__(config)

class C (B):
def __init__(self, config):
self.z = 0
super(C, self).__init__(config)

This works here because each of the initialisation
operations is self-contained. It might not work so well
in real life if some of the base class state needs to be
initialised before the subclass initialisation can be
performed. However, it's worth considering -- I came
across the same sort of problem several times in
PyGUI, and I usually managed to solve it by carefully
arranging initialisations before and after the super
call.

If you can't use that solution, I would suggest you
keep the __init__ and setConfig operations separate,
and live with having to call setConfig after creating
an object. Factory functions could be provided if
you were doing this a lot.

--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg

Jul 18 '05 #2
Dan Perl wrote:
So far, so good! But let's assume that I want to change the __init__
methods so that they take a configuration as an argument so the objects are
created and configured in one step, like this:
alpha = A(config)


One way would be to make the setConfig call only
in the root class, and perform the initialisation
that it depends on *before* making the super call
in each __init__ method, i.e.

class A (object):
def __init__(self, config):
self.x = 0
self.setConfig(config)

class B (A):
def __init__(self, config):
self.y = 0
super(B, self).__init__(config)

class C (B):
def __init__(self, config):
self.z = 0
super(C, self).__init__(config)

This works here because each of the initialisation
operations is self-contained. It might not work so well
in real life if some of the base class state needs to be
initialised before the subclass initialisation can be
performed. However, it's worth considering -- I came
across the same sort of problem several times in
PyGUI, and I usually managed to solve it by carefully
arranging initialisations before and after the super
call.

If you can't use that solution, I would suggest you
keep the __init__ and setConfig operations separate,
and live with having to call setConfig after creating
an object. Factory functions could be provided if
you were doing this a lot.

--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg

Jul 18 '05 #3
Thank you very much, Greg, that does the job! Somehow I couldn't see it and
I needed someone to point out to me.

Dan

"Greg Ewing" <gr**@cosc.canterbury.ac.nz> wrote in message
news:31*************@individual.net...
Dan Perl wrote:
So far, so good! But let's assume that I want to change the __init__
methods so that they take a configuration as an argument so the objects
are created and configured in one step, like this:
alpha = A(config)


One way would be to make the setConfig call only
in the root class, and perform the initialisation
that it depends on *before* making the super call
in each __init__ method, i.e.

class A (object):
def __init__(self, config):
self.x = 0
self.setConfig(config)

class B (A):
def __init__(self, config):
self.y = 0
super(B, self).__init__(config)

class C (B):
def __init__(self, config):
self.z = 0
super(C, self).__init__(config)

This works here because each of the initialisation
operations is self-contained. It might not work so well
in real life if some of the base class state needs to be
initialised before the subclass initialisation can be
performed. However, it's worth considering -- I came
across the same sort of problem several times in
PyGUI, and I usually managed to solve it by carefully
arranging initialisations before and after the super
call.

If you can't use that solution, I would suggest you
keep the __init__ and setConfig operations separate,
and live with having to call setConfig after creating
an object. Factory functions could be provided if
you were doing this a lot.

--
Greg Ewing, Computer Science Dept,
University of Canterbury, Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg

Jul 18 '05 #4
Thank you very much, Greg, that does the job! Somehow I couldn't see it and
I needed someone to point out to me.

Dan

"Greg Ewing" <gr**@cosc.canterbury.ac.nz> wrote in message
news:31*************@individual.net...
Dan Perl wrote:
So far, so good! But let's assume that I want to change the __init__
methods so that they take a configuration as an argument so the objects
are created and configured in one step, like this:
alpha = A(config)


One way would be to make the setConfig call only
in the root class, and perform the initialisation
that it depends on *before* making the super call
in each __init__ method, i.e.

class A (object):
def __init__(self, config):
self.x = 0
self.setConfig(config)

class B (A):
def __init__(self, config):
self.y = 0
super(B, self).__init__(config)

class C (B):
def __init__(self, config):
self.z = 0
super(C, self).__init__(config)

This works here because each of the initialisation
operations is self-contained. It might not work so well
in real life if some of the base class state needs to be
initialised before the subclass initialisation can be
performed. However, it's worth considering -- I came
across the same sort of problem several times in
PyGUI, and I usually managed to solve it by carefully
arranging initialisations before and after the super
call.

If you can't use that solution, I would suggest you
keep the __init__ and setConfig operations separate,
and live with having to call setConfig after creating
an object. Factory functions could be provided if
you were doing this a lot.

--
Greg Ewing, Computer Science Dept,
University of Canterbury, Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg

Jul 18 '05 #5
Dan Perl wrote:
Here is a problem I am having trouble with and I hope someone in this group
will suggest a solution. First, some code that works. 3 classes that are
derived from each other (A->B->C), each one implementing only 2 methods,
__init__ and setConfig.
-------------------------------------------------------
#!/usr/bin/python
class A (object):
def __init__(self):
super(A, self).__init__()
self.x = 0
def setConfig(self, config):
self.x += config['x']

class B (A):
def __init__(self):
super(B, self).__init__()
self.y = 0
def setConfig(self, config):
super(B, self).setConfig(config)
self.y += config['y']

class C (B):
def __init__(self):
super(C, self).__init__()
self.z = 0
def setConfig(self, config):
super(C, self).setConfig(config)
self.z += config['z']

config = {'x':1, 'y':2, 'z':3}
alpha = A()
alpha.setConfig(config)
print alpha.x
beta = B()
beta.setConfig(config)
print beta.x, beta.y
beta.setConfig(config)
print beta.x, beta.y
gamma = C()
gamma.setConfig(config)
print gamma.x, gamma.y, gamma.z
gamma.setConfig(config)
print gamma.x, gamma.y, gamma.z
--------------------------------------------------

The output from that code is:
1
1 2
2 4
1 2 3
2 4 6

So far, so good! But let's assume that I want to change the __init__
methods so that they take a configuration as an argument so the objects are
created and configured in one step, like this:
alpha = A(config)

How can the code be changed to implement this? It is tempting to modify the
__init__ methods like this:
class A (object):
def __init__(self, config):
super(A, self).__init__(config)
self.x = 0
A.setConfig(self, config)

However, if implemented this way, the output is:
1
2 2
3 4
3 4 3
4 6 6

This shows that setConfig gets called more than once because both __init__
and setConfig are cooperative methods.

I have been thinking about this for a day now and I cannot find a good
solution. I imagine this would be a common problem and that there would be
a recipe somewhere, but I couldn't find one (I looked in the Python
Cookbook). I've thought of creating 2 separate methods instead of
setConfig, one that does the "local" configuration and another one that
takes care of invoking super. But I don't like the idea of creating 2
methods in each class and I haven't been able to think of a way to solve the
problem with just one change in the base class that would be inherited by
all the subclasses.

Anyone has any ideas? Thanks.


What about using an attribute in the base class to remember whether
initial config has been done? This seems to work:

#!/usr/bin/python
class A (object):
def __init__(self, config):
self.x = 0
self.configinitialized = False
super(A, self).__init__()
if not self.configinitialized:
self.setConfig(config)
def setConfig(self, config):
self.x += config['x']
self.configset = True

class B (A):
def __init__(self, config):
self.y = 0
super(B, self).__init__(config)
def setConfig(self, config):
super(B, self).setConfig(config)
self.y += config['y']

class C (B):
def __init__(self, config):
self.z = 0
super(C, self).__init__(config)
def setConfig(self, config):
super(C, self).setConfig(config)
self.z += config['z']

config = {'x':1, 'y':2, 'z':3}
alpha = A(config)
print alpha.x
beta = B(config)
print beta.x, beta.y
beta.setConfig(config)
print beta.x, beta.y
gamma = C(config)
print gamma.x, gamma.y, gamma.z
gamma.setConfig(config)
print gamma.x, gamma.y, gamma.z
Jul 18 '05 #6
Dan Perl wrote:
Here is a problem I am having trouble with and I hope someone in this group
will suggest a solution. First, some code that works. 3 classes that are
derived from each other (A->B->C), each one implementing only 2 methods,
__init__ and setConfig.
-------------------------------------------------------
#!/usr/bin/python
class A (object):
def __init__(self):
super(A, self).__init__()
self.x = 0
def setConfig(self, config):
self.x += config['x']

class B (A):
def __init__(self):
super(B, self).__init__()
self.y = 0
def setConfig(self, config):
super(B, self).setConfig(config)
self.y += config['y']

class C (B):
def __init__(self):
super(C, self).__init__()
self.z = 0
def setConfig(self, config):
super(C, self).setConfig(config)
self.z += config['z']

config = {'x':1, 'y':2, 'z':3}
alpha = A()
alpha.setConfig(config)
print alpha.x
beta = B()
beta.setConfig(config)
print beta.x, beta.y
beta.setConfig(config)
print beta.x, beta.y
gamma = C()
gamma.setConfig(config)
print gamma.x, gamma.y, gamma.z
gamma.setConfig(config)
print gamma.x, gamma.y, gamma.z
--------------------------------------------------

The output from that code is:
1
1 2
2 4
1 2 3
2 4 6

So far, so good! But let's assume that I want to change the __init__
methods so that they take a configuration as an argument so the objects are
created and configured in one step, like this:
alpha = A(config)

How can the code be changed to implement this? It is tempting to modify the
__init__ methods like this:
class A (object):
def __init__(self, config):
super(A, self).__init__(config)
self.x = 0
A.setConfig(self, config)

However, if implemented this way, the output is:
1
2 2
3 4
3 4 3
4 6 6

This shows that setConfig gets called more than once because both __init__
and setConfig are cooperative methods.

I have been thinking about this for a day now and I cannot find a good
solution. I imagine this would be a common problem and that there would be
a recipe somewhere, but I couldn't find one (I looked in the Python
Cookbook). I've thought of creating 2 separate methods instead of
setConfig, one that does the "local" configuration and another one that
takes care of invoking super. But I don't like the idea of creating 2
methods in each class and I haven't been able to think of a way to solve the
problem with just one change in the base class that would be inherited by
all the subclasses.

Anyone has any ideas? Thanks.


What about using an attribute in the base class to remember whether
initial config has been done? This seems to work:

#!/usr/bin/python
class A (object):
def __init__(self, config):
self.x = 0
self.configinitialized = False
super(A, self).__init__()
if not self.configinitialized:
self.setConfig(config)
def setConfig(self, config):
self.x += config['x']
self.configset = True

class B (A):
def __init__(self, config):
self.y = 0
super(B, self).__init__(config)
def setConfig(self, config):
super(B, self).setConfig(config)
self.y += config['y']

class C (B):
def __init__(self, config):
self.z = 0
super(C, self).__init__(config)
def setConfig(self, config):
super(C, self).setConfig(config)
self.z += config['z']

config = {'x':1, 'y':2, 'z':3}
alpha = A(config)
print alpha.x
beta = B(config)
print beta.x, beta.y
beta.setConfig(config)
print beta.x, beta.y
gamma = C(config)
print gamma.x, gamma.y, gamma.z
gamma.setConfig(config)
print gamma.x, gamma.y, gamma.z
Jul 18 '05 #7
This is almost the same code as Greg's with the only difference being that
test for configuration having been done. But the test is unnecessary. I
don't see how setConfig could be invoked in the super of the base class (A),
so such a test would be relevant only in subclasses, if they DO invoke
setConfig. But it's better if they just don't invoke it, which makes for a
much cleaner solution. Thanks anyway.

BTW, you named the attribute configinitialized in one place and configSet in
the other place. Which proves that the test is redundant, because it does
work anyway as is.

I had a similar solution, where I was invoking setConfig only if the class
of self is the same as the class where __init__ is defined, something like
this:
class A (object):
def __init__(self, config):
self.x = 0
super(A, self).__init__()
if A == self.__class__:
self.setConfig(config)

I didn't like it though because it has to be done like this in every
subclass's __init__. And it's a problem that I didn't realize at the time
if a subclass just does not need to override __init__ (then the
configuration is just not set).

Dan

"David Fraser" <da****@sjsoft.com> wrote in message
news:co**********@ctb-nnrp2.saix.net...

What about using an attribute in the base class to remember whether
initial config has been done? This seems to work:

#!/usr/bin/python
class A (object):
def __init__(self, config):
self.x = 0
self.configinitialized = False
super(A, self).__init__()
if not self.configinitialized:
self.setConfig(config)
def setConfig(self, config):
self.x += config['x']
self.configset = True

class B (A):
def __init__(self, config):
self.y = 0
super(B, self).__init__(config)
def setConfig(self, config):
super(B, self).setConfig(config)
self.y += config['y']

class C (B):
def __init__(self, config):
self.z = 0
super(C, self).__init__(config)
def setConfig(self, config):
super(C, self).setConfig(config)
self.z += config['z']

config = {'x':1, 'y':2, 'z':3}
alpha = A(config)
print alpha.x
beta = B(config)
print beta.x, beta.y
beta.setConfig(config)
print beta.x, beta.y
gamma = C(config)
print gamma.x, gamma.y, gamma.z
gamma.setConfig(config)
print gamma.x, gamma.y, gamma.z

Jul 18 '05 #8
This is almost the same code as Greg's with the only difference being that
test for configuration having been done. But the test is unnecessary. I
don't see how setConfig could be invoked in the super of the base class (A),
so such a test would be relevant only in subclasses, if they DO invoke
setConfig. But it's better if they just don't invoke it, which makes for a
much cleaner solution. Thanks anyway.

BTW, you named the attribute configinitialized in one place and configSet in
the other place. Which proves that the test is redundant, because it does
work anyway as is.

I had a similar solution, where I was invoking setConfig only if the class
of self is the same as the class where __init__ is defined, something like
this:
class A (object):
def __init__(self, config):
self.x = 0
super(A, self).__init__()
if A == self.__class__:
self.setConfig(config)

I didn't like it though because it has to be done like this in every
subclass's __init__. And it's a problem that I didn't realize at the time
if a subclass just does not need to override __init__ (then the
configuration is just not set).

Dan

"David Fraser" <da****@sjsoft.com> wrote in message
news:co**********@ctb-nnrp2.saix.net...

What about using an attribute in the base class to remember whether
initial config has been done? This seems to work:

#!/usr/bin/python
class A (object):
def __init__(self, config):
self.x = 0
self.configinitialized = False
super(A, self).__init__()
if not self.configinitialized:
self.setConfig(config)
def setConfig(self, config):
self.x += config['x']
self.configset = True

class B (A):
def __init__(self, config):
self.y = 0
super(B, self).__init__(config)
def setConfig(self, config):
super(B, self).setConfig(config)
self.y += config['y']

class C (B):
def __init__(self, config):
self.z = 0
super(C, self).__init__(config)
def setConfig(self, config):
super(C, self).setConfig(config)
self.z += config['z']

config = {'x':1, 'y':2, 'z':3}
alpha = A(config)
print alpha.x
beta = B(config)
print beta.x, beta.y
beta.setConfig(config)
print beta.x, beta.y
gamma = C(config)
print gamma.x, gamma.y, gamma.z
gamma.setConfig(config)
print gamma.x, gamma.y, gamma.z

Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Ricky Romaya | last post by:
Hi, Are there any ways to get multiple inheritace in PHP4? For example, I have 3 parent class, class A, B, and C. I want class X to inherit all those 3 classes. Consider merging those 3 classes...
0
by: Dan Perl | last post by:
Here is a problem I am having trouble with and I hope someone in this group will suggest a solution. First, some code that works. 3 classes that are derived from each other (A->B->C), each one...
14
by: Axel Straschil | last post by:
Hello! Im working with new (object) classes and normaly call init of ther motherclass with callin super(...), workes fine. No, I've got a case with multiple inherance and want to ask if this...
13
by: John Perks and Sarah Mount | last post by:
Trying to create the "lopsided diamond" inheritance below: >>> class B(object):pass >>> class D1(B):pass >>> class D2(D1):pass >>> class D(D1, D2):pass Traceback (most recent call last): File...
20
by: km | last post by:
Hi all, In the following code why am i not able to access class A's object attribute - 'a' ? I wishto extent class D with all the attributes of its base classes. how do i do that ? thanks in...
4
by: Alex Hunsley | last post by:
I've seen a few discussion about the use of 'super' in Python, including the opinion that 'super' should only be used to solve inheritance diamond problem. (And that a constructor that wants to...
14
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at...
5
by: Thomas Girod | last post by:
Hi. I think I'm missing something about multiple inheritance in python. I've got this code. class Foo: def __init__(self): self.x = "defined by foo" self.foo = None
6
by: burningodzilla | last post by:
Hi all - I'm preparing to dive in to more complex application development using javascript, and among other things, I'm having a hard time wrapping my head around an issues regarding "inheritance"...
3
by: johanatan | last post by:
When I first heard about these new features, I was very excited as it would have (if implemented as I had expected) rendered mimicking multiple inheritance almost painless in C#. Unfortunately,...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.