Created
November 12, 2015 03:02
-
-
Save t-redactyl/d9c906e11391d0885978 to your computer and use it in GitHub Desktop.
Code associated with blog post:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def name_print(cat): | |
'''Print the name of the cat.''' | |
print "The cat is called %s." % cat.name | |
name_print(felix) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
felix.name = "Felix" | |
print felix.name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cats = ['Whiskers', 'Felix', 'Charley', 'Rosebud', 'Biddy'] | |
print cats |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cats.remove('Whiskers') | |
cats.append('Mr. Paws') | |
print cats |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Cat(object): | |
'''A Cat with name, temperament and weight components.''' | |
pass | |
felix = Cat() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Older_Cat(Cat): | |
'''A Cat aged over 8 years with name, temperament and | |
weight components.''' | |
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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