Re: [Tutor] OOD - Another class question

2010-03-01 Thread C.T. Matsumoto

James Reynolds wrote:
I have another question related to OOD. What I have is a module with 
one 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).


When I do this, I am told: AttributeError: 'HillBuilder' object has no 
attribute 'MountainBuilder'


The question is, what am I doing wrong?

Here is an example:

class MountainBuilder(object):
def __init__(self, mountain):
self.mountain = mountain
self.mountain_func
   self.pinetree_func


  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
  self.hill_func


  def hill_func(self)
hill= do stuff to self.mountain
grassyknoll = MountainBuilder.mountain_func(hill)

return grassy knoll



do stuff with grassy knoll



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
  

The line grassyknoll = MountainBuilder.mountain ...

Since you inherit MountainBuilder you call the mountain_func (which is a 
method :-) ) with self:

grassyknoll = self.mountain_func(hill)

All methods inside the MountainBuilder can be called as if they are a 
part of the HillBuilder (thus inheritance).


Cheers,

T
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] OOD - Another class question

2010-02-28 Thread Alan Gauld


"James Reynolds"  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/ 



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] OOD - Another class question

2010-02-28 Thread Steve Willoughby
On Sun, Feb 28, 2010 at 06:24:09PM -0500, James Reynolds wrote:
> I have another question related to OOD. What I have is a module with one
> 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).

I think you're confusing containers with inheritance.

> class MountainBuilder(object):
> def __init__(self, mountain):
> self.mountain = mountain
> self.mountain_func <--- what's this?
>self.pinetree_func  <--- what's this?
> 
> class HillBuilder(MountainBuilder):
> def __init__(self, mountain):
>   OptionLoad.__init__(self, mountain)
>   self.MountainBuilder.mountain_func

There is no MountainBuilder attribute in HillBuilder.  Rather,
HillBuilder is a refined type of a MountainBuilder, and inherits
everything MountainBuilders have, plus what you change or add
to that base in HillBuilder.  

You're treating it like it's a separate class which contains
a MountainBuilder object inside it as an attribute.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] OOD - Another class question

2010-02-28 Thread James Reynolds
I have another question related to OOD. What I have is a module with one
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).

When I do this, I am told: AttributeError: 'HillBuilder' object has no
attribute 'MountainBuilder'

The question is, what am I doing wrong?

Here is an example:

class MountainBuilder(object):
def __init__(self, mountain):
self.mountain = mountain
self.mountain_func
   self.pinetree_func


  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
  self.hill_func


  def hill_func(self)
hill= do stuff to self.mountain
grassyknoll = MountainBuilder.mountain_func(hill)

return grassy knoll


do stuff with grassy knoll
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor