[Tutor] OOD - Another class question
Alan Gauld
alan.gauld at btinternet.com
Mon Mar 1 02:13:16 CET 2010
"James Reynolds" <eire1130 at gmail.com> wrote
> parent class and two child classes. Some stuff is done to the object that
> is
> passed to the function in one of the child classes and this then calls a
> function from the global class passing local variables (from the child
> class).
You really need to tighten up on the terminology because the
imprecision is part of your problems understanding the concepts.
You pass an object to a method of a child class which calls
a method in the parent class. You pass instance variables
of the child class as arguments to the parent class method..
> When I do this, I am told: AttributeError: 'HillBuilder' object has no
> attribute 'MountainBuilder'
> The question is, what am I doing wrong?
Confusing inheritance with composition.
Your child class does not have an attribute MountainBuilder,
it *is* a MountainBuilder. self within the method referes to your
subclass of MountainBuilder so you only need self.
> class MountainBuilder(object):
> def pinetree_func(self, knoll)
> do stuff to knoll
> return knoll
> def mountain_func(self, hill)
> knoll = hill * 2
> pinetree = pintree_func(knoll)
> return hill
>
> class HillBuilder(MountainBuilder):
> def __init__(self, mountain):
> OptionLoad.__init__(self, mountain)
> self.MountainBuilder.mountain_func
This should be
self.mountain_func(mountain)
> def hill_func(self)
> hill= do stuff to self.mountain
> grassyknoll = MountainBuilder.mountain_func(hill)
And this should be
grassyknoll = self.mountain_func(hill)
mountain func as a method of MountainBuilder is also
a method of HillBuilder (thats what inheritance means)
so you access it through self.
Take a look at the BankAccount example in the OOP
topic of my tutor for more examples of subclasses
calling superclass methods
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list