run this program:
return error:
Traceback (most recent call last):
File "python", line 13, in <module>
TypeError: __init__() takes exactly 3 arguments (4 given)
Add as following(the red):
# Class definition
class Animal(object):
"""Makes cute animals."""
# For initializing our instance objects
def __init__(self, name, age, is_hungry): # add is_hungry
self.name = name
self.age = age
self.is_hungry = is_hungry # add is_hungry
# Note that self is only used in the __init__()
# function definition; we don't need to pass it
# to our instance objects.
zebra = Animal("Jeffrey", 2, True)
giraffe = Animal("Bruce", 1, False)
panda = Animal("Chad", 7, True)
print zebra.name, zebra.age, zebra.is_hungry
print giraffe.name, giraffe.age, giraffe.is_hungry
print panda.name, panda.age, panda.is_hungry
Run again:
Jeffrey 2 True
Bruce 1 False
Chad 7 True
Class Scope
Another important aspect of Python classes is scope. The scope of a variable is the context in which it's visible to the program.
It may surprise you to learn that not all variables are accessible to all parts of a Python program at all times. When dealing with classes, you can have variables that are available everywhere (global variables), variables that are only available to members of a certain class (member variables), and variables that are only available to particular instances of a class (instance variables).
The same goes for functions: some are available everywhere, some are only available to members of a certain class, and still others are only available to particular instance objects.
Instructions
Check out the code in the editor. Note that each individual animal gets its own name and age (since they're all initialized individually), but they all have access to the member variable is_alive, since they're all members of theAnimal class. Click Save & Submit Code to see the output!
class Animal(object):
"""Makes cute animals."""
is_alive = True # member variable
def __init__(self, name, age):
self.name = name
self.age = age
zebra = Animal("Jeffrey", 2) #instance variable
giraffe = Animal("Bruce", 1) #instance variable
panda = Animal("Chad", 7) #instance variable
print zebra.name, zebra.age, zebra.is_alive
print giraffe.name, giraffe.age, giraffe.is_alive
print panda.name, panda.age, panda.is_alive
run this program:
Jeffrey 2 True
Bruce 1 True
Chad 7 True
A Methodical Approach
When a class has its own functions, those functions are called methods. You've already seen one such method:__init__(). But you can also define your own methods!
Instructions
Add a method, description, to your Animal class. Using two separate print statements, it should print out the name andage of the animal it's called on. Then, create an instance ofAnimal, hippo (with whatever name and age you like), and call its description method.
class Animal(object):
"""Makes cute animals."""
is_alive = True
def __init__(self, name, age):
self.name = name
self.age = age
# Add your method here!
def description(self):
print self.name
print self.age
hippo = Animal("dog", 5)
hippo.description()
run this program:
dog
5
They're Multiplying!
A class can have any number ofmember variables. These are variables that are available to all members of a class.
hippo = Animal("Jake", 12)
cat = Animal("Boots", 3)
print hippo.is_alive
hippo.is_alive = False
print hippo.is_alive
print cat.is_alive
1. In the example above, we create two instances of anAnimal.
2. Then we print out True, the default value stored in hippo'sis_alive member variable.
3. Next, we set that to False and print it out to make sure.
4. Finally, we print out True, the value stored in cat'sis_alive member variable. We only changed the variable in hippo, not in cat.
Let's add another member variable to Animal.
Instructions
1. After line 3, add a second member variable called healththat contains the string "good".
2. Then, create two newAnimals: sloth and ocelot. (Give them whatever names and ages you like.)
3. Finally, on three separate lines, print out the health of your hippo, sloth, and ocelot.
class Animal(object):
"""Makes cute animals."""
is_alive = True
health = "good"
def __init__(self, name, age):
self.name = name
self.age = age
# Add your method here!
def description(self):
print self.name
print self.age
hippo = Animal("dog", 5)
hippo.description()
sloth = Animal("cat", 2)
ocelot = Animal("cow", 1)
print hippo.health
print sloth.health
print ocelot.health
run this program:
dog
5
good
good
good
It's Not All Animals and Fruits
Classes like Animal and Fruitmake it easy to understand the concepts of classes and instances, but you probably won't see many zebras or lemons in real-world programs.
However, classes and objects are often used to model real-world objects. The code in the editor is a more realistic demonstration of the kind of classes and objects you might find in commercial software. Here we have a basicShoppingCart class for creating shopping cart objects for website customers; though basic, it's similar to what you'd see in a real program.
Instructions
Create an instance ofShoppingCart called my_cart. Initialize it with any values you like, then use the add_itemmethod to add an item to your cart.
class ShoppingCart(object):
"""Creates shopping cart objects
for users of our fine website."""
items_in_cart = {}
def __init__(self, customer_name):
self.customer_name = customer_name
def add_item(self, product, price):
"""Add product to the cart."""
if not product in self.items_in_cart:
self.items_in_cart[product] = price
print product + " added."
else:
print product + " is already in the cart."
def remove_item(self, product):
"""Remove product from the cart."""
if product in self.items_in_cart:
del self.items_in_cart[product]
print product + " removed."
else:
print product + " is not in the cart."
my_cart = ShoppingCart("Apple")
my_cart.add_item("Fruit", 2)
run this program:
Fruit added.
Warning: Here Be Dragons
Inheritance is a tricky concept, so let's go through it step by step.
Inheritance is the process by which one class takes on the attributes and methods of another, and it's used to express an is-a relationship. For example, a Panda is a bear, so a Panda class could inherit from a Bear class. However, a Toyota is not a Tractor, so it shouldn't inherit from the Tractor class (even if they have a lot of attributes and methods in common). Instead, both Toyota and Tractor could ultimately inherit from the same Vehicle class.
Instructions
Check out the code in the editor. We've defined a class, Customer, as well as a ReturningCustomer class that inherits from Customer. Note that we don't define the display_cart method in the body of ReturningCustomer, but it will still have access to that method via inheritance. Click Save & Submit Code to see for yourself!
class Customer(object):
"""Produces objects that represent customers."""
def __init__(self, customer_id):
self.customer_id = customer_id
def display_cart(self):
print "I'm a string that stands in for the contents of your shopping cart!"
class ReturningCustomer(Customer):
"""For customers of the repeat variety."""
def display_order_history(self):
print "I'm a string that stands in for your order history!"
monty_python = ReturningCustomer("ID: 12345")
monty_python.display_cart()
monty_python.display_order_history()
run this program:
I'm a string that stands in for the contents of your shopping cart!
I'm a string that stands in for your order history!
Inheritance Syntax
In Python, inheritance works like this:
class DerivedClass(BaseClass):
# code goes here
where DerivedClass is the new class you're making and BaseClass is the class from which that new class inherits.
Instructions
On lines 1-4, we've created a class named Shape.
1. Create your own class, Triangle, that inherits from Shape, like this:
2. class Triangle(Shape):
3. # code goes here
4. Inside the Triangle class, write an__init__() function that takes four arguments: self, side1, side2, andside3.
5. Inside the __init__() function, setself.side1 = side1, self.side2 = side2, and self.side3 = side3.
Click "Stuck? Get a hint!" for an example.
class Shape(object):
"""Makes shapes!"""
def __init__(self, number_of_sides):
self.number_of_sides = number_of_sides
# Add your Triangle class below!
class Triangle(Shape):
# code goes here
def __init__(self, side1, side2, side3):
self.side1 = side1
self.side2 = side2
self.side3 = side3
Override!
Sometimes you'll want one class that inherits from another to not only take on the methods and attributes of its parent, but to override one or more of them.
class Employee(object):
def __init__(self, name):
self.name = name
def greet(self, other):
print "Hello, %s" % other.name
class CEO(Employee):
def greet(self, other):
print "Get back to work, %s!" % other.name
ceo = CEO("Emily")
emp = Employee("Steve")
emp.greet(ceo)
# Hello, Emily
ceo.greet(emp)
# Get back to work, Steve!
Rather than have a separate greet_underling method for our CEO, we override (or re-create) the greet method on top of the base Employee.greet method. This way, we don't need to know what type of Employee we have before we greet another Employee.
Instructions
1. Create a new class, PartTimeEmployee, that inherits from Employee.
2. Give your derived class acalculate_wage method that overridesEmployee's. It should take self and hoursas arguments.
3. BecausePartTimeEmployee.calculate_wage overridesEmployee.calculate_wage, it still needs to setself.hours = hours.
4. It should return the part-time employee's number of hours worked multiplied by 12.00 (that is, they get $12.00 per hour instead of $20.00).
class Employee(object):
"""Models real-life employees!"""
def __init__(self, employee_name):
self.employee_name = employee_name
def calculate_wage(self, hours):
self.hours = hours
return hours * 20.00
# Add your code below!
class PartTimeEmployee(Employee):
def calculate_wage(self, hours):
self.hours = hours
return hours * 12
This Looks Like a Job For...
On the flip side, sometimes you'll be working with a derived class (or subclass) and realize that you've overwritten a method or attribute defined in that class' base class (also called a parent or superclass) that you actually need. Have no fear! You can directly access the attributes or methods of a superclass with Python's built-in super call.
The syntax looks like this:
class Derived(Base):
def m(self):
return super(Derived, self).m()
Where m() is a method from the base class.
Instructions
First, inside your PartTimeEmployee class:
1. Add a new method called full_time_wage with the arguments self and hours.
2. That method should return the result of a super call to the calculate_wage method of PartTimeEmployee's parent class. Use the example above for help.
Then, after your class:
1. Create an instance of thePartTimeEmployee class called milton. Don't forget to give it a name.
2. Finally, print out the result of calling his full_time_wage method. You should see his wage printed out at $20.00 per hour! (That is, for 10 hours, the result should be 200.00.)
class Employee(object):
"""Models real-life employees!"""
def __init__(self, employee_name):
self.employee_name = employee_name
def calculate_wage(self, hours):
self.hours = hours
return hours * 20.00
# Add your code below!
class PartTimeEmployee(Employee):
def calculate_wage(self, hours):
self.hours = hours
return hours * 12
def full_time_wage(self, hours):
return super(PartTimeEmployee, self).calculate_wage(hours)
milton = PartTimeEmployee("Bob")
print milton.full_time_wage(10)
run this program:
200.0
Class Basics
First things first: let's create a class to work with.
Instructions
Create a class, Triangle. Its __init__()method should take self, angle1, angle2, and angle3 as arguments. Make sure to set these appropriately in the body of the__init__() method (see the Hint for more).
class Triangle(object):
def __init__(self, angle1, angle2, angle3):
self.angle1 = angle1
self.angle2 = angle2
self.angle3 = angle3
Class It Up
Great! Now let's add a member variable and a method to our class.
Instructions
Inside the Triangle class:
1. Create a variable namednumber_of_sides and set it equal to 3.
2. Create a method named check_angles. The sum of a triangle's three angles should return True if the sum of self.angle1,self.angle2, and self.angle3 is equal 180, and False otherwise.
class Triangle(object):
def __init__(self, angle1, angle2, angle3):
self.angle1 = angle1
self.angle2 = angle2
self.angle3 = angle3
number_of_sides = 3
def check_angles():
if self.angle1 + self.angle2 + self.angle3 == 180:
return True
else:
return False
run this program:
return:”Oops,try again.Make sure to pass self as an argument to your check_angles method!
Add self:
class Triangle(object):
def __init__(self, angle1, angle2, angle3):
self.angle1 = angle1
self.angle2 = angle2
self.angle3 = angle3
number_of_sides = 3
def check_angles(self):
if self.angle1 + self.angle2 + self.angle3 == 180:
return True
else:
return False
Instantiate an Object
Let's go ahead and create an instance of our Triangle class.
1. Create a variable namedmy_triangle and set it equal to a new instance of your Triangleclass. Pass it three angles that sum to 180 (e.g. 90, 30, 60).
2. Print outmy_triangle.number_of_sides
3. Print outmy_triangle.check_angles()
class Triangle(object):
def __init__(self, angle1, angle2, angle3):
self.angle1 = angle1
self.angle2 = angle2
self.angle3 = angle3
number_of_sides = 3
def check_angles(self):
if self.angle1 + self.angle2 + self.angle3 == 180:
return True
else:
return False
my_triangle = Triangle(90, 30, 60)
print my_triangle.number_of_sides
print my_triangle.check_angles()
run this program:
3
True
Inheritance
Finally, let's create an Equilateral class that inherits from our Triangle class. (An equilateral triangle is a triangle whose angles are all 60˚, which also means that its three sides are equal in length.)
Instructions
1. Create a class namedEquilateral that inherits fromTriangle.
2. Inside Equilateral, create a member variable named angleand set it equal to 60.
3. Create an __init__()function with only the parameterself, and set self.angle1,self.angle2, and self.angle3equal to self.angle (since an equilateral triangle's angles will always be 60˚).
class Triangle(object):
def __init__(self, angle1, angle2, angle3):
self.angle1 = angle1
self.angle2 = angle2
self.angle3 = angle3
number_of_sides = 3
def check_angles(self):
if self.angle1 + self.angle2 + self.angle3 == 180:
return True
else:
return False
my_triangle = Triangle(90, 30, 60)
print my_triangle.number_of_sides
print my_triangle.check_angles()
class Equilateral(Triangle):
angle = 60
def __init__(self):
self.angle1 = self.angle
self.angle2 = self.angle
self.angle3 = self.angle