Вы находитесь на странице: 1из 62

INTRODUCTION TO CLASSES

Why Use Classes?


Python is an object-oriented programming language, which means
it manipulates programming constructs called objects. You can
think of an object as a single data structure that contains data as
well as functions; the functions of an object are called
its methods. For example, any time you call

len("Eric")
Python is checking to see whether the string object you passed it
has a length, and if it does, it returns the value associated with
that attribute. When you call

my_dict.items()
Python checks to see if my_dict has an items()method (which all
dictionaries have) and executes that method if it finds it.

But what makes "Eric" a string and my_dict a dictionary? The fact


that they’re instances of the str and dict classes, respectively. A
class is just a way of organizing and producing objects with
similar attributes and methods.
Instructions
1.
Check out the code in the editor to the right. We’ve defined our
own class, Fruit, and created a lemoninstance.When you’re ready,
click Run to get started creating classes and objects of your own.
INTRODUCTION TO CLASSES

Class Syntax
A basic class consists only of the class keyword, the name of the
class, and the class from which the new class inherits in
parentheses. (We’ll get to inheritance soon.) For now, our classes
will inherit from the object class, like so:

class NewClass(object):
# Class magic here
This gives them the powers and abilities of a Python object. By
convention, user-defined Python class names start with a capital
letter.
Instructions
1.
Create a class called Animal in the editor. For now, in the body of
your class, use the pass keyword. (pass doesn’t do anything, but it’s
useful as a placeholder in areas of your code where Python
expects an expression.)
INTRODUCTION TO CLASSES

Classier Classes
We’d like our classes to do more than… well, nothing, so we’ll
have to replace our pass with something else.

You may have noticed in our example back in the first exercise
that we started our class definition off with an odd-looking
function: __init__(). This function is required for classes, and it’s
used to initialize the objects it creates. __init__()always takes at
least one argument, self, that refers to the object being created.
You can think of __init__() as the function that “boots up” each
object the class creates.
Instructions
1.
Remove the pass statement in your class definition, then go ahead
and define an __init__() function for your Animal class. Pass it the
argument selffor now; we’ll explain how this works in greater
detail in the next section. Finally, put the pass into the body of
the __init__() definition, since it will expect an indented block.
Hint
Your __init__() function should look something like this:

def __init__(self):
pass
Question
What is the difference between a class and an object?

Answer
Think of a class as a blueprint and an object as the result of creating
what that blueprint specified.
Classes allow us to define a general state and behavior for the
objects we want to make. This is a super powerful way to approach
programming because it allows for us to generalize our code to work
for many situations!
For example, we might make a Car class that has things you might
find in any car: wheel_count, gas_tank_size, and a seat_count of at
least one for the driver to sit in. When we create an object of
type Car, we might make a mustang object (notice it’s lowercase, as
is convention when instantiating objects of a class). Our mustang has
its own values for those variables because it’s different from
other Cars, but still has a lot of the same properties. We could make an
object for any model of car!
Same basic structure, different values. That’s the use of a class!
INTRODUCTION TO CLASSES

Let's Not Get Too Selfish


Excellent! Let’s make one more tweak to our class definition, then go
ahead and instantiate (create) our first object.

So far, __init__() only takes one parameter: self. This is a Python


convention; there’s nothing magic about the word self. However, it’s
overwhelmingly common to use self as the first parameter in __init__(), so
you should do this so that other people will understand your code.

The part that is magic is the fact that self is the first parameter passed


to __init__(). Python will use the first parameter that __init__() receives to
refer to the object being created; this is why it’s often called self, since
this parameter gives the object being created its identity.

Instructions
1.
Let’s do two things in the editor:

 Pass __init__() a second parameter, name.


 In the body of __init__(), let the function know
that name refers to the created object’s name by
typing self.name = name. (This will become crystal clear in
the next section.)

Hint
Your syntax should look like this:

class Animal(object):
def __init__(self, name):
# Set the name parameter here!
Question
What is self?

Answer
self’s purpose may not seem very obvious at first, especially if you’ve
never used an object-oriented programming language before!
Try to imagine a class named Car that has the basic structure that
all cars share in common. It defines a basic acceleration behavior, but
not how fast it accelerates or what the max speed is, as that varies
from car to car. It defines a start method, but not the specifics of
how it starts, as that varies as well.
Now imagine that we’ve created Car objects of all the models we can
think of. So now we have a mustang object, a tesla_model_x object,
etc. Upon creation we can now specify how a particular model
accelerates or starts. Inside of our Car’s __init()__ method we
use self to refer to the current car model so we can assign values
like top speeds and whatnot.
The current car model is one attribute of the car
instance. self refers to the instance in context scope, that is
the current instance, i.e., the instance upon which the call is made.

my_car = Car('Toyota Corolla', 'crimson', '88')

When we access the attributes or methods of this


instance, self becomes my_car so that this instance is the context.

my_car.model
my_car.color
my_car.mpg
INTRODUCTION TO CLASSES

Instantiating Your First Object


Perfect! Now we’re ready to start creating objects.

We can access attributes of our objects using dot notation. Here’s


how it works:

class Square(object):
def __init__(self):
self.sides = 4

my_shape = Square()
print my_shape.sides

1. First we create a class named Square with an attribute sides.


2. Outside the class definition, we create a new instance
of Square named my_shape and access that attribute
using my_shape.sides.

Instructions
1.
Outside the Animal class definition, create a variable
named zebra and set it equal to Animal("Jeffrey").

Then print out zebra‘s name.
Hint
You can create a new Animal object named "Jeffrey" like this:

zebra = Animal("Jeffrey")
You can print out "Jeffrey"‘s name like this:

print zebra.name
INTRODUCTION TO CLASSES

More on __init__() and self


Now that you’re starting to understand how classes and objects
work, it’s worth delving a bit more into __init__() and self. They can
be confusing!

As mentioned, you can think of __init__() as the method that


“boots up” a class’ instance object: the init bit is short for
“initialize.”

The first argument __init__() gets is used to refer to the instance


object, and by convention, that argument is called self. If you add
additional arguments—for instance, a name and age for your animal
—setting each of those equal to self.name and self.age in the body
of __init__() will make it so that when you create an instance object
of your Animal class, you need to give each instance a name and an
age, and those will be associated with the particular instance you
create.
Instructions
1.
Check out the examples in the editor. See how __init__() “boots
up” each object to expect a name and an age, then
uses self.name and self.age to assign those names and ages to each
object? Add a third attribute, is_hungry to __init__(), and click Run to
see the results.
Hint
Your code should look something like this:

def __init__(self, name, age, is_hungry)


self.name = name
self.age = age
self.is_hungry = is_hungry
Question
Why do we not pass anything for self?

Answer
Python knows that the __init__() method’s first parameter, typically
called self, is assigned to the object you create when you initialize it.
That way you can use self throughout your class to accomplish
things with that particular object!
If you do pass something for self when initializing an object, you’ll
get an error about the number of arguments provided and the number
of arguments expected. This is because whatever you pass starts by
giving value to the second, third, fourth, and so on, parameters.
In our Animal example, we have 4 parameters total: self, name, age,
is_hungry. However, if we try to pass it 4 arguments when we call it,
we’re actually passing it 5, because it automatically passes self!
INTRODUCTION TO CLASSES

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
1.
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 the Animal class. Click Run to see the
output!
INTRODUCTION TO CLASSES

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
1.
Add a method, description, to your Animal class. Using two
separate printstatements, it should print out the nameand age of the
animal it’s called on. Then, create an instance of Animal, hippo (with
whatever name and age you like), and call its description method.
Hint
Remember to pass self as an argument to description. Otherwise,
printing self.nameand self.age won’t work, since Python won’t know
which self (that is, which object) you’re talking about!

Your method should look something like this:

def description(self):
print self.name
print self.age
After that, all you need to do is create a hippoand call its
description method with hippo.description()!
Question
How can I create a new Animal object and use its description method?

Answer
Remember, creating a new object of a class looks like this:
object_name = ClassName("arguments_here")
Then, to use the description() method of our new Animal, we simply
call that method attached to our new object, like this:
object_name.method_name()
INTRODUCTION TO CLASSES

They're Multiplying!
A class can have any number of member 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 an Animal.


2. Then we print out True, the default value stored in
hippo’s is_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’s is_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 health that
contains the string "good".

Then, create two new Animals: slothand ocelot. (Give them whatever


names and ages you like.)

Finally, on three separate lines, printout the health of


your hippo, sloth, and ocelot.
Hint
You can add your member variable right under is_alive, like so:

is_alive = True
health = "good"
You can print out your hippo‘s health with

print hippo.health
INTRODUCTION TO CLASSES

It's Not All Animals and Fruits


Classes like Animal and Fruit make 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 basic ShoppingCartclass for creating
shopping cart objects for website customers; though basic, it’s
similar to what you’d see in a real program.
Instructions
1.
Create an instance of ShoppingCartcalled my_cart. Initialize it with any
values you like, then use the add_itemmethod to add an item to your
cart.
Hint
Since the ShoppingCart class has an __init__() method that takes a
customer name, I’d create my cart like so:

my_cart = ShoppingCart("Eric")
Calling the add_item() method might then be:

my_cart.add_item("Ukelele", 10)
INTRODUCTION TO CLASSES

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
1.
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 Run to see for yourself!
Question
What makes a language object oriented?

Answer
We’ve learned so far about a few of the concepts that are core in
almost any object-oriented language. The big three features typically
associated with object orientation are polymorphism, encapsulation,
and inheritance.
For the purposes of this course, it’s plenty to understand that objects
can inherit from each other, can define how their data is accessed, and
how to create and use classes and objects.
If you’re up for a challenge, a quick Google search for those other
object-oriented properties will provide a rabbit hole to keep you busy
for days! To start you off, this StackOverflow post’s top
answer 605 gives some great insights about what defines object
orientation.

29
Definitions for Object-Orientation are of course a huge can of worms, but here
are my 2 cents:
To me, Object-Orientation is all about objects that collaborate by sending
messages. That is, to me, the single most important trait of an object-
oriented language.

If I had to put up an ordered list of all the features that an object-oriented


language must have, it would look like this:

1. Objects sending messages to other objects


2. Everything is an Object
3. Late Binding
4. Subtype Polymorphism
5. Inheritance or something similarly expressive, like Delegation
6. Encapsulation
7. Information Hiding
8. Abstraction
Obviously, this list is very controversial, since it excludes a great variety of
languages that are widely regarded as object-oriented, such
as Java, C# and C++, all of which violate points 1, 2 and 3. However, there is
no doubt that those languages allow for object-oriented programming (but so
does C) and even facilitate it (which C doesn't). So, I have come to call
languages that satisfy those requirements "purely object-oriented".
As archetypical object-oriented languages I would name Self and Newspeak.
Both satisfy the above-mentioned requirements. Both are inspired by and
successors to Smalltalk, and both actually manage to be "more OO" in some
sense. The things that I like about Self and Newspeak are that both take the
message sending paradigm to the extreme (Newspeak even more so than
Self).
In Newspeak, everything is a message send. There are no instance variables,
no fields, no attributes, no constants, no class names. They are all emulated
by using getters and setters.
In Self, there are no classes, only objects. This emphasizes, what OO
is really about: objects, not classes.
INTRODUCTION TO CLASSES

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
1.
On lines 1-4, we’ve created a class named Shape.

 Create your own class, Triangle, that inherits from Shape,


like this:
 class Triangle(Shape):
# code goes here

 Inside the Triangle class, write an __init__() function that


takes four arguments: self, side1, side2, and side3.
 Inside the __init__() function, set self.side1 =
side1, self.side2 = side2, and self.side3 = side3.

Click “Stuck? Get a hint!” for an example.


Hint
Your code should look something like this:

class Triangle(Shape):
def __init__(self, side1, side2, side3):
self.side1 = side1
self.side2 = side2
self.side3 = side3
Question
How will Triangle objects know which init method to use?

Answer
In this exercise we’ve defined a new class called Triangle that
inherits from the base class Shape. Both classes define
an __init__() method! Luckily, Python is smart enough to see that we
intended to use the initialize method in our Triangle class when
creating a new Triangle object.
Python checks the object that called the method and sees that it has a
method of the same name that then takes priority over the one in the
base class.
In the case of __init__() being defined twice, it sees that we’re
initializing a Triangle, so we use that method.
INTRODUCTION TO CLASSES

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_underlingmethod 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.

Give your derived class a calculate_wagemethod that


overrides Employee‘s. It should take self and hours as arguments.

Because PartTimeEmployee.calculate_wageoverrides Employee.calculate_wage, it
still needs to set self.hours = hours.

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).
Hint
In the example code above, we created an
overriding CEO.greet method. It had the same arguments
as Employee.greet. You’ll want to add a calculate_wage() method to
your PartTimeEmployee class, and it should also take self and hours as
arguments. Instead of returning hours * 20.00, though, it should
return hours * 12.00.
Question
What does it mean to override a method?

Answer
The point of overriding is to allow different behavior from the same
function in different classes.
In our Employee and PartTimeEmployee example, we want both
employees to be paid, so we create a method to calculate their wages.
And since a part time employee is definitely a type of Employee, it
makes sense for them to derive a lot of the same functionality.
However! That doesn’t mean their pay should be calculated in the
same way. So we override the calculate_wage method to behave as
we want it to for PartTimeEmployee objects.
By defining a method with the same name as found in the base
class, Employee, we are overriding the functionality so that
any PartTimeEmployees will have their wages calculated appropriately.
INTRODUCTION TO CLASSES

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
1.
First, inside your PartTimeEmployee class:

 Add a new method called full_time_wagewith the


arguments self and hours.
 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:

 Create an instance of the PartTimeEmployeeclass


called milton. Don’t forget to give it a name.
 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.)

Hint
You super call should look something like this:

def full_time_wage(self, hours):


return super(PartTimeEmployee, self).method(args)
Where method is the method you want (calculate_wage) and args are the
arguments that method takes.
INTRODUCTION TO CLASSES

Class Basics
First things first: let’s create a class to work with.
Instructions
1.
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).
Hint
Make sure your Triangle inherits from object. Remember, class
syntax looks like this:

class ClassName(object):
def __init__(args):
# Set self.args = args
INTRODUCTION TO CLASSES

Class It Up
Great! Now let’s add a member variable and a method to our
class.
Instructions
1.
Inside the Triangle class:

 Create a variable named number_of_sides and set it equal


to 3.
 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.

Hint
The check_angles method should look something like this:

def check_angles(self):
if (self.angle1 + self.angle2 + self.angle3 == 180):
return True
else:
return False
INTRODUCTION TO CLASSES

Instantiate an Object
Let’s go ahead and create an instance of our Triangle class.
Instructions
1.
Create a variable named my_triangleand set it equal to a new
instance of your Triangle class. Pass it three angles that sum to 180
(e.g. 90, 30, 60).

Print out my_triangle.number_of_sides

Print out my_triangle.check_angles()
Hint
Remember, we can instantiate an object like so:

instance = Class(args)
Where args are the arguments __init__()takes, not including self.
INTRODUCTION TO CLASSES

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 named Equilateral that inherits from Triangle.

Inside Equilateral, create a member variable named angle and set it


equal to 60.

Create an __init__() function with only the parameter self, and


set self.angle1, self.angle2, and self.angle3 equal to self.angle (since an
equilateral triangle’s angles will always be 60˚).
Hint
Remember, inheritance looks like this:

class DerivedClass(BaseClass):
# Your code here
where DerivedClass is the new class you’re making, and BaseClass is
the class it inherits from.
CLASSES

Class basics
Classes can be very useful for storing complicated objects with
their own methods and variables. Defining a class is much like
defining a function, but we use the class keyword instead. We also
use the word object in parentheses because we want our classes
to inherit the object class. This means that our class has all the
properties of an object, which is the simplest, most basic class.
Later we’ll see that classes can inherit other, more complicated
classes. An empty class would look like this:

class ClassName(object):
# class statements go here
Instructions
1.
Define a new class named “Car”. For now, since we have to put
something inside the class, use the pass keyword.
Question
What are the properties of ‘object’, the simplest and most basic class?

Answer
In Python, the object from which all classes inherit their most basic
properties is called object. This allows us to customize everything
about our new class, including how to initialize it, how to set
descriptors, accepting arguments, and more.
For an in-depth view, take a look at the top answer of this
StackOverflow post 643 about the object we inherit from!
CLASSES

Create an instance of a class


We can use classes to create new objects, which we say
are instances of those classes.

Creating a new instance of a class is as easy as saying:

newObject = ClassName()
Instructions
1.
Below your Car class, create a new object named my_car that is an
instance of Car.
CLASSES

Class member variables


Classes can have member variables that store information about
each class object. We call them member variables since they are
information that belongs to the class object.

Creating member variables and assigning them initial values is as


easy as creating any other variable:

class ClassName(object):
memberVariable = "initialValue"
Instructions
1.
Inside your Car class, replace the passstatement with a new
member variable named condition and give it an initial value of the
string "new".
Question
What makes member variables useful?

Answer
Member variables are those that you want to be the same across all
objects of a class. In our Carexample, all cars should start off
in new condition, so we set condition = "new" as a member variable.
However, things that might differ from car to car are
their name and year_model! So for those things that are not the same
across the board, we create instance variables.
CLASSES

Calling class member variables


Each class object we create has its own set of member variables.
Since we’ve created an object my_car that is an instance of
the Car class, my_car should already have a member variable
named condition. This attribute gets assigned a value as soon
as my_car is created.
Instructions
1.
At the end of your code, use a printstatement to display
the condition of my_car.
Hint
Since the attribute condition belongs to the object my_car, you’ll need
to use dot notationto access the object’s member
variable: my_car.condition.
Question
How can I use dot notation to access a member variable?

Answer
If you’re trying to access a member variable, you do not need
parentheses at the end, otherwise it looks for a method with the name
you typed.
To access a member variable, we simply type the name of the variable
after a dot connected to the object we want to access, like this:
print my_object.property_name
CLASSES

Initializing a class
There is a special function named __init__()that gets called
whenever we create a new instance of a class. It exists by default,
even though we don’t see it. However, we can define our
own __init__() function inside the class, overwriting the default
version. We might want to do this in order to provide more input
variables, just like we would with any other function.

The first argument passed to __init__() must always be the


keyword self - this is how the object keeps track of itself internally
- but we can pass additional variables after that.

In order to assign a variable to the class (creating a member


variable), we use dot notation. For instance, if we
passed newVariable into our class, inside the __init__() function we
would say:

self.new_variable = new_variable
Instructions
1.
Define the __init__() function of the Car class to take four inputs:
self, model, color, and mpg. Assign the last three inputs to
member variables of the same name by using the self keyword.

Then, modify the object my_car to provide the following inputs at


initialization:

model = "DeLorean"
color = "silver"
mpg = 88
You don’t need to include the selfkeyword when you create an
instance of a class, because self gets added to the beginning of
your list of inputs automatically by the class definition.
Hint
Creating an instance of a class with many initialization variables
looks the same as calling a function with many inputs; put all the
values in parentheses, separated by commas.

In the body of __init__(), you’d set the model like this:


def __init__(self, model, color, mpg):
self.model = model

Question
How does init know what values to assign to instance variables?

Answer
If we make an __init__() method that accepts the same parameters
as our Car class, like this: (self, model, color, mpg), then these are
the values that will be provided whenever you create a
new Car object.
When a new Car object is created, the values provided as arguments
for those parameters are then given to you for use inside
the __init__() method. That’s where we’re able to create new
instance variables with the structure: self.variable_name =
variable_name. The left side uses self to tell Python to create a new
instance variable for this particular object we just made, and then the
right side is the value we store in that new variable, which is just the
argument value.
CLASSES

Referring to member variables


Calling class member variables works the same whether those
values are created within the class (like our car’s condition) or
values are passed into the new object at initialization. We use dot
notation to access the member variables of classes since those
variables belong to the object.

For instance, if we had created a member variable


named new_variable, a new instance of the class
named new_object could access this variable by saying:

new_object.new_variable
Instructions
1.
Now that you’ve created my_car print its member variables:

 First print the model of my_car. Click “Stuck? Get a hint!” for an


example.
 Then print out the color of my_car.
 Then print out the mpg of my_car.

Hint
To print my_car‘s model, you’d type:

print my_car.model
CLASSES

Creating class methods


Besides member variables, classes can also have their own
methods. For example:

class Square(object):
def __init__(self, side):
self.side = side

def perimeter(self):
return self.side * 4
The perimeter() class method is identical to defining any other
function, except that it is written inside of the Square class
definition.

Just like when we defined __init__(), you need to provide self as the


first argument of any class method.
Instructions
1.
Inside the Car class, add a method named display_car to Car that will
reference the Car’s member variables to return the string, "This is
a [color] [model] with [mpg] MPG."  You can use the str() function to turn
your mpg into a string when creating the display string.

Replace the individual print statements with a


single print command that displays the result of
calling my_car.display_car()
Hint
Remember, in order to access member variables of a class (even
while inside of that class), we have to use the self keyword and
dot notation to specify that we mean the member variable that
belongs to the class.
CLASSES

Modifying member variables


We can modify variables that belong to a class the same way that
we initialize those member variables. This can be useful when we
want to change the value a variable takes on based on something
that happens inside of a class method.
Instructions
1.
Inside the Car class, add a method drive_car that sets self.condition to
the string "used".

Remove the call to my_car.display_car()and instead print only


the condition of your car.

Then drive your car by calling the drive_car method.

Finally, print the condition of your car again to see how its value


changes.
Question
Why do I get an error after running my code to print and modify
condition?

Answer
The steps must be followed in exactly the order asked for in the
instructions, otherwise you’ll get errors saying that you didn’t print or
change something properly.
In this order, we should:

1. print our car’s condition instance variable by accessing it


using dot notation, like this: print my_car.condition
2. Change the value stored in condition by using the method we
just created. To use an object’s method, simply use dot notation,
like this: my_car.method_name(). You don’t need to type anything
else on the line other than that, since the method is just doing
what it does (changing a value) and then moving on.
3. print the car’s condition one last time to see the change it
made!
CLASSES

Inheritance
One of the benefits of classes is that we can create more
complicated classes that inherit variables or methods from
their parent classes. This saves us time and helps us build more
complicated objects, since these child classes can also include
additional variables or methods.

We define a “child” class that inherits all of the variables and


functions from its “parent” class like so:

class ChildClass(ParentClass):
# new variables and functions go here
Normally we use object as the parent class because it is the most
basic type of class, but by specifying a different class, we can
inherit more complicated functionality.
Instructions
1.
Create a class ElectricCar that inherits from Car. Give your new class
an __init__() method of that includes a battery_type member variable
in addition to the model, color and mpg.

Then, create an electric car named my_car with a "molten


salt"battery_type. Supply values of your choice for the other three
inputs (model, color and mpg).
Hint
Redefining a method of a “child” class is as simple as including a
definition for that function inside the “child” class; this version will
take precedence over the inherited version.

Remember to include the self keyword as the first input when you


define the __init__()method!
Question
Why do I have to re-define all of my instance variables in
ElectricCar’s init?
Answer
If we override a method, then the new method must be defined in such
a way that it does everything you want it to do, which may include
everything from the original method! If that’s the case, we’ve learned
about calling the parent method by using the super keyword, so that’ll
save us some time.
We can just call the parent Car __init__() method by using
the super keyword that we learned about earlier, and then add
whatever we like afterwards, like this:

def __init__(self, model, color, mpg, battery_type):


super(ElectricCar, self).__init__(model, color, mpg)
# Add code to define battery_type instance variable here!

That saves us the trouble of rewriting those lines of code we already


wrote, while allowing us to add more to the end for our battery_type!
CLASSES

Overriding methods
Since our ElectricCar is a more specialized type of Car, we can
give the ElectricCar its own drive_car() method that has different
functionality than the original Car class’s.
Instructions
1.
Inside ElectricCar add a new method drive_car that changes the
car’s condition to the string "like new".

Then, outside of ElectricCar, print the condition of my_car

Next, drive my_car by calling the drive_car function

Finally, print the condition of my_caragain


Hint
This should be very similar to what you did in the second exercise
of this section.
CLASSES

Building useful classes


Chances are, you won’t be designing Car classes in the real world
anytime soon. Usually, classes are most useful for holding and
accessing abstract collections of data.

One useful class method to override is the built-


in __repr__() method, which is short for representation; by providing
a return value in this method, we can tell Python how to represent
an object of our class (for instance, when using a print statement).
Instructions
1.
Define a Point3D class that inherits from object

Inside the Point3D class, define an __init__() function that


accepts self, x, y, and z, and assigns these numbers to the member
variables self.x, self.y, self.z

Define a __repr__() method that returns "(%d, %d, %d)" % (self.x, self.y,


self.z). This tells Python to represent this object in the following
format: (x, y, z).

Outside the class definition, create a variable


named my_point containing a new instance of Point3D with x=1, y=2,
and z=3.

Finally, print my_point.
Hint
When defining a new __repr__(), return a string value that uses the
member variables of the class to display the 3D point properly.
You can use the str() function to put these numbers in the proper
string.

For advanced users: For more information on __repr__ and other


special methods see this Python documentation. Note the slight
difference between the __repr__ and __str__methods.
Question
How can I define and use a repr method?

Answer
In our class Print3D, there are a few key points to make
our __repr__() method useful.
Be sure to pass self to any class method, including __repr__(), like
this: def __repr__(self):
Inside we need to define how our object is printed. Whenever the built-
in print function receives our object, it looks for
a __repr__() method, and if it’s found, it prints the return value to
the screen! So we just return what the instructions ask: "(%d, %d,
%d)" % (self.x, self.y, self.z)

Вам также может понравиться