[Tutor] class with objects
Alan Gauld
alan.gauld at btinternet.com
Mon Oct 12 01:05:57 CEST 2009
<shellcom3 at juno.com> wrote
>I want to display the ship default value for zero and display the ship's
> initial fuel level. Also have a method called status that displays an
> object's name and fuel values.
So far so good.
> I want to have several Ship objects and call their status() methods
> to test various aspects of the class constructor.
Now I'm confused.
Does status "test various aspects of the class constructor" or does it
"display an objects name and fuel values"? These are not the same thing...
> Here's my code:
>
> class Ship(object):
> """A spaceship"""
> total = 0
> def __init__(self, name, fuel = 0):
>
> print "My spaceship has arrived! The",name
> self.name = name
> self.fuel = fuel
>
> print "My fuel level is", fuel
>
> def status():
> Ship.total += 1
What is the puropse of thoe above line?
Why is it part of status()?
> print "The total number of objects is", Ship.total
> status = staticmethod(status)
And why is it a staticmethod?
> ship = Ship("Galaxia")
> print "\nCreating objects."
> ship1 = Ship("object 1")
> ship2 = Ship("object 2")
> ship3 = Ship("object 3")
> Ship.status()
What do you expect the output to be?
And what is it really? Are they different?
Why?
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list