length = 3
price = 5.99
count = 0
name = "George"
poem = "Roses are Red\nViolets are Blue"
option = "n"
discount = True
show_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 = True
skin = "scales"
def __init__(self, fish_name, fish_color):
self.name = fish_name
self.color = fish_color
def 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 = species
spencer = Fish("Goldfish")
susan = Fish("Flounder")
class Goldfish:
breathes_in_water = True
skin = "scales"
def move(self, speed):
print "Swimming upright %s!" % speed
class Flounder:
breathes_in_water = True
skin = "scales"
def move(self, speed):
print "Swimming sideways %s!" % speed
class Fish:
skin = "scales"
class Goldfish(Fish): # <- Fish specified as parent
def 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 Blue
skin = "scales"
class Flounder(Fish):
pass # Inherits color = "Blue" from parent
class Goldfish(Fish):
color = "Gold" # Override color for Goldfish
>> susan = Flounder()
>> susan.color
"Blue"
>> spencer = Goldfish()
>> spencer.color
"Gold"
class Fish:
pass
class Goldfish(Fish):
pass
class Flounder(Fish):
pass
>> spencer = Goldfish()
>> isinstance(spencer, Fish)
True
>> isinstance(spencer, Goldfish)
True
>> isinstance(spencer, Flounder)
False
class Fish(object):
pass
class Goldfish(Fish):
pass
class Aquarium(object):
fish = []
def __init__(self, fish):
self.fish = fish
class Fish(object):
color = "Blue"
def __init__(self, name):
self.name = name
class 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!