Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code associated with blog post:
def name_print(cat):
'''Print the name of the cat.'''
print "The cat is called %s." % cat.name
name_print(felix)
felix.name = "Felix"
print felix.name
class Cat(object):
'''A Cat with name, temperament and weight components.'''
def name_print(self):
'''Print the name of the cat.'''
print "The cat is called %s." % self.name
felix = Cat()
felix.name = "Felix"
felix.name_print()
class Cat(object):
'''A Cat with name, temperament and weight components.'''
def __init__(self, name):
'''A new cat with name value name.'''
self.name = name
def name_print(self):
'''Print the name of the cat.'''
print "The cat is called %s." % self.name
felix = Cat("Felix")
felix.name_print()
class Cat(object):
'''A Cat with name, temperament and weight components.'''
def __init__(self, name):
'''A new Cat with a name (string).'''
self.name = name
def __str__(self):
'''Print the name of the cat.'''
return "The cat is called %s." % self.name
felix = Cat("Felix")
print felix
class Cat(object):
'''A Cat with name, temperament and weight components.'''
def __init__(self, name, temperament, weight):
'''A new Cat with a name (string), temperament (string)
and weight (float).'''
self.name = name
self.temperament = temperament
self.weight = weight
def __str__(self):
'''Print the Cat object.'''
return "The cat is called %s. Its temperament is %s. It weighs %s kgs." \
% (self.name, self.temperament, self.weight)
def is_friendly(self):
'''Prints the temperament of the cat.'''
return self.temperament == "friendly"
def if_weighs_more(self, cutoff_weight):
'''Returns if cat weighs more than a specified value.'''
return self.weight > cutoff_weight
felix = Cat("Felix", "friendly", 6)
print felix
felix.is_friendly()
felix.if_weighs_more(5)
cats = ['Whiskers', 'Felix', 'Charley', 'Rosebud', 'Biddy']
print cats
cats.remove('Whiskers')
cats.append('Mr. Paws')
print cats
class Cat(object):
'''A Cat with name, temperament and weight components.'''
pass
felix = Cat()
class Older_Cat(Cat):
'''A Cat aged over 8 years with name, temperament and
weight components.'''
pass
class Older_Cat(Cat):
'''A Cat aged over 8 years with name, temperament and
weight components.'''
def __init__(self, name, temperament, weight):
'''A new Older_Cat with a name (string), temperament
(string) and weight (float).'''
Cat.__init__(self, name, temperament, weight)
rosebud = Older_Cat("Rosebud", "cranky", 4)
print rosebud
rosebud.is_friendly()
rosebud.if_weighs_more(5)
class Older_Cat(Cat):
'''A Cat aged over 8 years with name, temperament and
weight components.'''
def __init__(self, name, temperament, weight, illness):
'''A new Older_Cat with a name (string), temperament
(string) and weight (float).'''
Cat.__init__(self, name, temperament, weight)
self.illness = illness
def has_illness(self):
'''Describes if the Older_Cat has any health conditions.'''
if self.illness != "none":
print "%s has %s. Please consult care plan." \
% (self.name, self.illness)
else:
print "%s has no health conditions." % self.name
rosebud = Older_Cat("Rosebud", "cranky", 4, "arthritis")
print rosebud
rosebud.has_illness()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment