[Tutor] class with objects

shellcom3 at juno.com shellcom3 at juno.com
Sun Oct 11 21:42:38 CEST 2009

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. I want to have several Ship objects and call their status() methods to test various aspects of the class constructor.

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
        print "The total number of objects is", Ship.total
    status = staticmethod(status) 

ship = Ship("Galaxia")

print "\nCreating objects."
ship1 = Ship("object 1")
ship2 = Ship("object 2")
ship3 = Ship("object 3")

Find Top-Rated Pavers
Get competing bids on any driveway, patio or walk need. Free quotes!

More information about the Tutor mailing list