L
lm401
I'm trying to work with the following idea:
class animal:
def __init__(self, weight, colour):
self.weight = weight
self.colour = colour
class bird(animal):
def __init__(self, wingspan):
self.wingspan = wingspan
print self.weight, self.colour, self.wingspan
class fish(animal):
def __init__(self, length):
self.length = length
print self.weight, self.colour, self.length
So basically I have a base class (animal) that has certain attributes.
When animal is inherited by a specific instance, further attributes are
added, but I need to have access to the original attributes (weight,
colour). When I run my code from within the derived class, self.weight
and self.colour are not inherited (although methods are inherited as I
would have expected).
It seems from reading the newsgroups that a solution might be to
declare weight and colour as global variables in the class animal:
class animal:
pass
myanimal = animal()
myanimal.weight = 4
myanimal.colour = 'blue'
But this is not exactly what I want to do.
I'm not very experienced with OOP techniques, so perhaps what I'm
trying to do is not sensible. Does Python differ with regard to
inheritance of member variables from C++ and Java?
Thanks for any help,
Lorcan.
class animal:
def __init__(self, weight, colour):
self.weight = weight
self.colour = colour
class bird(animal):
def __init__(self, wingspan):
self.wingspan = wingspan
print self.weight, self.colour, self.wingspan
class fish(animal):
def __init__(self, length):
self.length = length
print self.weight, self.colour, self.length
So basically I have a base class (animal) that has certain attributes.
When animal is inherited by a specific instance, further attributes are
added, but I need to have access to the original attributes (weight,
colour). When I run my code from within the derived class, self.weight
and self.colour are not inherited (although methods are inherited as I
would have expected).
It seems from reading the newsgroups that a solution might be to
declare weight and colour as global variables in the class animal:
class animal:
pass
myanimal = animal()
myanimal.weight = 4
myanimal.colour = 'blue'
But this is not exactly what I want to do.
I'm not very experienced with OOP techniques, so perhaps what I'm
trying to do is not sensible. Does Python differ with regard to
inheritance of member variables from C++ and Java?
Thanks for any help,
Lorcan.