I'm trying to figure out how to roughly do what is below. Some things will only have foo
, some will have both foo
and bar
, and some will have foo
, bar
, and baz
. I would like to be able to either call Child.calc(10)
or GrandChild.calc(10)
and get the attributes set correctly as needed.
At the end of building out multiple instances of Initial, Child, and Grandchild, I would like to just call calc on any of them and have it work, regardless of what class they are. Initially I was going to do calc_initial, calc_child, calc_grandchild, but that seemed wrong.
Initial Code
class Initial:
def __init__(self, foo):
self.foo = foo
def Calc(self, amount):
self.TotalFoo = self.foo * amount
class Child(Initial):
def __init__(self, foo, bar):
Initial.__init__(self, foo)
self.bar = bar
def Calc(self, amount):
self.TotalBar = self.bar * amount
class GrandChild(Child):
def __init__(self, foo, bar, baz):
Child.__init__(self, foo, bar)
self.baz = baz
def Calc(self, amount):
self.TotalBaz = self.baz * amount
GrandChild(10, 20, 30)
GrandChild.Calc(10)
print(GrandChild.TotalFoo) # Want this to be 100
print(GrandChild.TotalBar) # Want this to be 200
print(GrandChild.TotalBaz) # Want this to be 300
Edited Code Thanks to mkrieger1
class Initial:
def __init__(self, foo):
self.foo = foo
def calc(self, amount):
self.total_foo = self.foo * amount
class Child(Initial):
def __init__(self, foo, bar):
Initial.__init__(self, foo)
self.bar = bar
def calc(self, amount):
Initial.calc(self, amount)
self.total_bar = self.bar * amount
class GrandChild(Child):
def __init__(self, foo, bar, baz):
Child.__init__(self, foo, bar)
self.baz = baz
def calc(self, amount):
Child.calc(self, amount)
self.total_baz = self.baz * amount
child = Child(2,4)
grandchild = GrandChild(10, 20, 30)
child.calc(10)
grandchild.calc(10)
print(child.total_foo) # Want this to be 20
print(child.total_bar) # Want this to be 40
print(grandchild.total_foo) # Want this to be 100
print(grandchild.total_bar) # Want this to be 200
print(grandchild.total_baz) # Want this to be 300
Trying to incorporate super but it feels like something is wrong. Results are correct but not sure.
class Initial:
def __init__(self, foo):
self.foo = foo
def calc(self, amount):
self.total_foo = self.foo * amount
class Child(Initial):
def __init__(self, foo, bar):
Initial.__init__(self, foo)
self.bar = bar
def calc(self, amount):
super().calc(amount)
self.total_bar = self.bar * amount
class GrandChild(Child):
def __init__(self, foo, bar, baz):
Child.__init__(self, foo, bar)
self.baz = baz
def calc(self, amount):
super().calc(amount)
self.total_baz = self.baz * amount
child = Child(2,4)
grandchild = GrandChild(10, 20, 30)
child.calc(10)
grandchild.calc(10)
print(child.total_foo) # Want this to be 20
print(child.total_bar) # Want this to be 40
print(grandchild.total_foo) # Want this to be 100
print(grandchild.total_bar) # Want this to be 200
print(grandchild.total_baz) # Want this to be 300
When trying to do elder's response
class Initial:
def __init__(self, foo):
self.foo = foo
def calc(self, amount):
self.total_foo = self.foo * amount
class Child(Initial):
def __init__(self, foo, bar):
super().__init__(foo)
self.bar = bar
def calc(self, amount):
self.total_bar = self.bar * amount
class GrandChild(Child):
def __init__(self, foo, bar, baz):
super().__init__(foo, bar)
self.baz = baz
def calc(self, amount):
self.total_baz = self.baz * amount
child = Child(2,4)
grandchild = GrandChild(10, 20, 30)
child.calc(10)
grandchild.calc(10)
print(child.total_foo) # Want this to be 20
print(child.total_bar) # Want this to be 40
print(grandchild.total_foo) # Want this to be 100
print(grandchild.total_bar) # Want this to be 200
print(grandchild.total_baz) # Want this to be 300
I get
26 child.calc(10)
27 grandchild.calc(10)
---> 28 print(child.total_foo) # Want this to be 20
29 print(child.total_bar) # Want this to be 40
30 print(grandchild.total_foo) # Want this to be 100
AttributeError: 'Child' object has no attribute 'total_foo'