[Tutor] Using Class methods

Alan Gauld alan.gauld at btinternet.com
Tue Jun 21 19:31:11 CEST 2011


"David Merrick" <merrickdav at gmail.com> wrote

>I need help using Class methods in another class. I have a class 
>called
> Critter and I want to create two critters in a farm  Class Farm and 
> use
> Class Critter's methods

Ok, You seem to be a bit confused by OOP, and
your code is using all sorts of features you probably
shouldn't be messing with yet.

Let's start with your main() method:

def main():
    crit1 = Critter("Sweetie")
    crit2 = Critter("Dave")
    farmlet = [crit1,crit2]

So far so good.

    farmlet.hunger = 0
    farmlet.boredom = 0

But here you are trying to access hunger and boredom
attributes of a list(farmlet) which doesn't have those
attributes. They are supposed to be part of your Farm
class. But you haven't got a Farm yet so you can't access
its attributes.

    farm = Farm.__str__(farmlet)

Now you assign the string representation of a Farm
to farm, but again you have no Farm object. And its
highly unusual to pass attributes to a __str__ method!
In fact its likely to cause serious problems if you ever
try to print the Farm!

So what about those attributes? Now we come to the
next problem, let's look at the class definition:

class Farm(Critter,hunger = 0,boredom = 0):

I have no idea where you got that syntax, but its wrong.
The parentheses in a class definition contain the list of
super classes from which you are inheriting.
A Farm is not a type of Critter, it has a collection of them.
And you cannot define attributes there, you need
to do that in the class body (for class level atrtributes),
or in the init method (for instance level attributes).

of critters...

    def __init__(farmlet, name):

A method needs to take the instance placeholder
variable - usually called self - as its first attribute.

If we add a self this implies that you need to create
a Farm with:

f = Farm(aFarmlet, aName)

But you never actually creaate a Farm object in your code...

        for critter in farmlet:
            critter.name = name
            critter.hunger = hunger
            critter.boredom = boredom

This bit would be OK except all the critters would have
the same name, hunger and boredom, which seems
unlikely? Especially since you gave them different
names when you created them(see above).

There are plenty other anomolies I could highlight but
as I say, I think you need to go back to first principles
and try again. This time forget about using properties,
and don't create any __xxx__ methods yet,
(except __init__!) - they just confuse things at this stage.

I would suggest you strip it right back to just defining
the two classes and their init methods to initialise the
data, nothing else.

Your basic logic is then to create some critters - which you
do already. Then create a Farm, passing the list of
critters into it. Then print out the farm's list to check
they are in there. Once you have done that add the
__str__ methods to aid printing. Once that works you
can start to think about addig the game logic and
supporting methods. But get the basics right first.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list