[Tutor] class question
Corey Richardson
kb1pkl at aim.com
Wed Jan 26 01:22:32 CET 2011
On 01/25/2011 06:26 PM, Elwin Estle wrote:
> Is it better to have one large sort of "do it all" class, or break the larger class up into smaller classes? Seems to me like the one large class would be clearer in some ways. I have something I am trying to do that have somewhere in the neighborhood of 20 attributes that all relate together, however there are sort of "clumps" of attributes that have a sub-relationship to each other and I wondered if they should be their own class, but I am not sure, assuming that is a good idea, how these smaller classes might work together. I have only the foggiest notion of inheritance and I'd kind of like to stay away from that aspect of things until I get a better grasp of individual classes.
>
If you're just learning, go ahead and make a 'do it all' class. Don't do
it later in your growth as a programmer though.
Inheritance works like this:
class Parent:
def __init__(self):
print "Parent initialised"
class Child(Parent):
pass
The parenthesis set the superclass or parent class. If you now do:
c = Child()
You should see "Parent initialised" printed to the terminal. It makes
all the 'parents' methods available to it. You can think of it as
copying all the code from the parent class into the child class. You can
overwrite the methods too:
class Animal:
def speak(self):
print self.noise
class Pig(Animal):
def __init__(self):
self.noise = "Oink!"
pig = Pig()
pig.speak()
Hope it helped,
~Corey
More information about the Tutor
mailing list