length = 3price = 5.99count = 0
name = "George"poem = "Roses are Red\nViolets are Blue"option = "n"
discount = Trueshow_help = False

(c)Tomo.Yun (www.yunphoto.net/en/)

class Fish:pass
>> Fish()
<... Fish instance at ...>
>> Fish()
<... Fish instance at ...>
>> a = Fish()
>> b = Fish()
>> a == b
False
class Fish:
breathes_in_water = True
skin = "scales"
>> myfish = Fish() >> myfish.skin "scales">> if myfish.breathes_in_water:... print "Glug glug!"Glug glug!
class Fish:def move(self, speed):print "swimming %s!" % speed
>> myfish = Fish()>> myfish.move("fast")swimming fast!
...def move(self, speed):...myfish.move("fast")
class Fish:def move(self, speed):print self.name + " is moving " + speed + "!">> myfish = Fish()>> myfish.name = "Spencer">> myfish.move("fast")Spencer is moving fast!
>> myfish = Fish()>> myfish.name = "Spencer">> myfish.color = "Gold">> myotherfish = Fish()>> myotherfish.name = "Susan">> myotherfish.color = "Blue"
class Fish:def __init__(self):print "Fish init!">> myfish = Fish()Fish init!>> Fish()Fish init!
class Fish:def __init__(self, fish_name):self.name = fish_name>> myfish = Fish("Spencer")>> myfish.name"Spencer"
class Fish:breathes_in_water = Trueskin = "scales"def __init__(self, fish_name, fish_color):self.name = fish_nameself.color = fish_colordef move(self, speed):print self.name + " is moving " + speed + "!">> spencer = Fish("Spencer", "Gold")>> print spencer.move("slowly")Spencer is moving slowly!
class Fish:def __init__(self, species):self.species = speciesspencer = Fish("Goldfish")susan = Fish("Flounder")
class Goldfish:breathes_in_water = Trueskin = "scales"def move(self, speed):print "Swimming upright %s!" % speedclass Flounder:breathes_in_water = Trueskin = "scales"def move(self, speed):print "Swimming sideways %s!" % speed
class Fish:skin = "scales"class Goldfish(Fish): # <- Fish specified as parentdef move(self, speed):print "Moving %s" % speed>> spencer = Goldfish()>> spencer.skin # <- So it has "skin" from Fish"scales"
class Fish:color = "Blue" # Default fish color is Blueskin = "scales"class Flounder(Fish):pass # Inherits color = "Blue" from parentclass Goldfish(Fish):color = "Gold" # Override color for Goldfish>> susan = Flounder()>> susan.color"Blue">> spencer = Goldfish()>> spencer.color"Gold"
class Fish:passclass Goldfish(Fish):passclass Flounder(Fish):pass>> spencer = Goldfish()>> isinstance(spencer, Fish)True>> isinstance(spencer, Goldfish)True>> isinstance(spencer, Flounder)False
class Fish(object):passclass Goldfish(Fish):pass
class Aquarium(object):fish = []def __init__(self, fish):self.fish = fishclass Fish(object):color = "Blue"def __init__(self, name):self.name = nameclass Goldfish(Fish):color = "Gold"class Flounder(Fish):pass>> my_fish = [Goldfish("Spencer"), Goldfish("Vladimir"), Flounder("Susan")]>> my_aquarium = Aquarium(my_fish)>> for fish in my_aquarium.fish:... print fish.name
class Aquarium(object):...def feed(self, food):for fish in self.fish:fish.eat(food)class Fish(object):...def eat(self, food):print self.name + " is eating " + food + "!"class Goldfish(Fish):pass>> my_aquarium = Aquarium([Goldfish("Spencer"), Goldfish("Susan")])>> my_aquarium.feed("flakes")Spencer is eating flakes!Susan is eating flakes!