Joe's ACA PreLab Reading

1. Jess | Intro to Classes & Inheritance


In [1]:
class Pet(object):
    
    def __init__(self, name, species):
        self.name = name
        self.species = species
    
    def getName(self):
        return self.name
    
    def getSpecies(self):
        return self.species
    
    def __str__(self):
        return "%s is a %s" % (self.name, self.species)

In [2]:
polly = Pet("Polly", "Parrot")

In [3]:
Pet.getName(polly)


Out[3]:
'Polly'

In [4]:
Pet.getSpecies(polly)


Out[4]:
'Parrot'

In [5]:
Pet.getName()


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-b114e8564cd5> in <module>()
----> 1 Pet.getName()

TypeError: unbound method getName() must be called with Pet instance as first argument (got nothing instead)

In [6]:
polly.getName()


Out[6]:
'Polly'

In [7]:
polly.getSpecies()


Out[7]:
'Parrot'

In [8]:
print polly


Polly is a Parrot

In [9]:
ginger = Pet("Ginger", "Cat")
print ginger.getName()
print ginger.getSpecies()
print ginger


Ginger
Cat
Ginger is a Cat

In [10]:
clifford = Pet("Clifford", "Dog")
print clifford.getName()
print clifford.getSpecies()
print clifford


Clifford
Dog
Clifford is a Dog

In [11]:
class Dog(Pet):
    
    def __init__(self, name, chases_cats):
        Pet.__init__(self, name, "Dog")
        self.chases_cats = chases_cats
    
    def chasesCats(self):
        return self.chases_cats

In [12]:
class Cat(Pet):
    
    def __init__(self, name, hates_dogs):
        Pet.__init__(self, name, "Cat")
        self.hates_dogs = hates_dogs
    
    def hatesDogs(self):
        return self.hates_dogs

In [13]:
mister_pet = Pet("Mister", "Dog")
mister_dog = Dog("Mister", True)

In [14]:
isinstance(mister_pet, Pet)


Out[14]:
True

In [15]:
isinstance(mister_dog, Pet)


Out[15]:
True

In [16]:
isinstance(mister_pet, Dog)


Out[16]:
False

In [17]:
isinstance(mister_dog, Dog)


Out[17]:
True

In [18]:
mister_dog.chasesCats()


Out[18]:
True

In [19]:
mister_pet.getName()


Out[19]:
'Mister'

In [20]:
mister_pet.getSpecies()


Out[20]:
'Dog'

In [21]:
mister_dog.getName()


Out[21]:
'Mister'

In [22]:
mister_dog.getSpecies()


Out[22]:
'Dog'

In [23]:
fido = Dog("Fido", True)
rover = Dog("Rover",False)
mittens = Cat("Mittens", True)
fluffy = Cat("Fluffy", False)

In [24]:
print fido
print rover
print mittens
print fluffy
print "%s chases cats: %s" % (fido.getName(), fido.chasesCats())
print "%s chases cats: %s" % (rover.getName(), rover.chasesCats())
print "%s hates dogs: %s" % (mittens.getName(), mittens.hatesDogs())
print "%s hates dogs: %s" % (fluffy.getName(), fluffy.hatesDogs())


Fido is a Dog
Rover is a Dog
Mittens is a Cat
Fluffy is a Cat
Fido chases cats: True
Rover chases cats: False
Mittens hates dogs: True
Fluffy hates dogs: False

Jessica ends here!!!


2. Jeff | Python Classes & OOP


In [25]:
class Customer(object):
    
    def __init__(self, name, balance=0.0):
        self.name = name
        self.balance = balance
    
    def withdraw(self, amount):
        if amount > self.balance:
            raise RuntimeError('No sufficient balance in your account.')
        self.balance -= amount
        return self.balance
    
    def deposit(self, amount):
        self.balance += amount
        return self.balance

In [26]:
jeff = Customer('Jeff Knupp', 1000)

In [27]:
jeff.withdraw(100)


Out[27]:
900

In [28]:
jeff.withdraw(1100)


---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-28-9f6ca1ccd0c2> in <module>()
----> 1 jeff.withdraw(1100)

<ipython-input-25-9b610c7bdd8b> in withdraw(self, amount)
      7     def withdraw(self, amount):
      8         if amount > self.balance:
----> 9             raise RuntimeError('No sufficient balance in your account.')
     10         self.balance -= amount
     11         return self.balance

RuntimeError: No sufficient balance in your account.

In [29]:
jeff.deposit(1000)


Out[29]:
1900

In [ ]:
# Same class in a different way
class Customer(object):
    
    def __init__():