Python Class Concepts
Python Class Concepts
Python Class Concepts
com/python3-object-oriented-programming/
Real Python
In this article you’ll pick up the following basic concepts of OOP in Python:
Python Classes
Object Instances
Defining and Working with Methods
OOP Inheritance
Free Bonus: Click here to get access to a free Python OOP Cheat Sheet that points
you to the best tutorials, videos, and books to learn more about Object-Oriented
Programming with Python.
For instance, an object could represent a person with a name property, age,
address, etc., with behaviors like walking, talking, breathing, and running. Or an
email with properties like recipient list, subject, body, etc., and behaviors like
adding attachments and sending.
The key takeaway is that objects are at the center of the object-oriented
programming paradigm, not only representing the data, as in procedural
programming, but in the overall structure of the program as well.
Classes in Python
1 of 16 11/26/18, 1:20 AM
Object-Oriented Programming (OOP) in Python 3 – Real Python https://realpython.com/python3-object-oriented-programming/
Focusing first on the data, each thing or object is an instance of some class.
The primitive data structures available in Python, like numbers, strings, and lists
are designed to represent simple things like the cost of something, the name of a
poem, and your favorite colors, respectively.
For example, let’s say you wanted to track a number of different animals. If you
used a list, the first element could be the animal’s name while the second element
could represent its age.
How would you know which element is supposed to be which? What if you had
100 different animals? Are you certain each animal has both a name and an age,
and so forth? What if you wanted to add other properties to these animals? This
lacks organization, and it’s the exact need for classes.
Classes are used to create new user-defined data structures that contain arbitrary
information about something. In the case of an animal, we could create an
Animal() class to track properties about the Animal like the name and age.
It’s important to note that a class just provides structure—it’s a blueprint for how
something should be defined, but it doesn’t actually provide any real content itself.
The Animal() class may specify that the name and age are necessary for defining
an animal, but it will not actually state what a specific animal’s name or age is.
It may help to think of a class as an idea for how something should be defined.
Put another way, a class is like a form or questionnaire. It defines the needed
information. After you fill out the form, your specific copy is an instance of the
class; it contains actual information relevant to you.
You can fill out multiple copies to create many different instances, but without the
form as a guide, you would be lost, not knowing what information is required.
Thus, before you can create individual instances of an object, we must first specify
what is needed by defining a class.
You start with the class keyword to indicate that you are creating a class, then you
add the name of the class (using CamelCase notation, starting with a capital letter.)
Also, we used the Python keyword pass here. This is very often used as a place
2 of 16 11/26/18, 1:20 AM
Object-Oriented Programming (OOP) in Python 3 – Real Python https://realpython.com/python3-object-oriented-programming/
holder where code will eventually go. It allows us to run this code without
throwing an error.
The (object) part in parentheses specifies the parent class that you are
inheriting from (more on this below.) In Python 3 this is no longer
necessary because it is the implicit default.
Instance Attributes
All classes create objects, and all objects contain characteristics called attributes
(referred to as properties in the opening paragraph). Use the __init__() method to
initialize (e.g., specify) an object’s initial attributes by giving them their default
value (or state). This method must have at least one argument as well as the self
variable, which refers to the object itself (e.g., Dog).
class Dog:
In the case of our Dog() class, each dog has a specific name and age, which is
obviously important to know for when you start actually creating different dogs.
Remember: the class is just for defining the Dog, not actually creating instances of
individual dogs with specific names and ages; we’ll get to that shortly.
Similarly, the self variable is also an instance of the class. Since instances of a
class have varying values we could state Dog.name = name rather than self.name =
name. But since not all dogs share the same name, we need to be able to assign
different values to different instances. Hence the need for the special self variable,
which will help to keep track of individual instances of each class.
NOTE: You will never have to call the __init__() method; it gets
called automatically when you create a new ‘Dog’ instance.
Class Attributes
While instance attributes are specific to each object, class attributes are the same
for all instances—which in this case is all dogs.
class Dog:
# Class Attribute
species = 'mammal'
3 of 16 11/26/18, 1:20 AM
Object-Oriented Programming (OOP) in Python 3 – Real Python https://realpython.com/python3-object-oriented-programming/
self.age = age
So while each dog has a unique name and age, every dog will be a mammal.
Instantiating Objects
Instantiating is a fancy term for creating a new, unique instance of a class.
For example:
>>>
>>> class Dog:
... pass
...
>>> Dog()
<__main__.Dog object at 0x1004ccc50>
>>> Dog()
<__main__.Dog object at 0x1004ccc90>
>>> a = Dog()
>>> b = Dog()
>>> a == b
False
We started by defining a new Dog() class, then created two new dogs, each
assigned to different objects. So, to create an instance of a class, you use the the
class name, followed by parentheses. Then to demonstrate that each instance is
actually different, we instantiated two more dogs, assigning each to a variable, then
tested if those variables are equal.
>>>
>>> class Dog:
... pass
...
>>> a = Dog()
>>> type(a)
<class '__main__.Dog'>
# Class Attribute
species = 'mammal'
4 of 16 11/26/18, 1:20 AM
Object-Oriented Programming (OOP) in Python 3 – Real Python https://realpython.com/python3-object-oriented-programming/
# Is Philo a mammal?
if philo.species == "mammal":
print("{0} is a {1}!".format(philo.name, philo.species))
NOTE: Notice how we use dot notation to access attributes from each
object.
Save this as dog_class.py, then run the program. You should see:
Philo is 5 and Mikey is 6.
Philo is a mammal!
These attributes are passed to the __init__ method, which gets called any time you
create a new instance, attaching the name and age to the object. You might be
wondering why we didn’t have to pass in the self argument.
This is Python magic; when you create a new instance of the class, Python
automatically determines what self is (a Dog in this case) and passes it to the
__init__ method.
class Dog:
# Class Attribute
species = 'mammal'
5 of 16 11/26/18, 1:20 AM
Object-Oriented Programming (OOP) in Python 3 – Real Python https://realpython.com/python3-object-oriented-programming/
# Output
print("The oldest dog is {} years old.".format(
get_biggest_number(jake.age, doug.age, william.age)))
Instance Methods
Instance methods are defined inside a class and are used to get the contents of an
instance. They can also be used to perform operations with the attributes of our
objects. Like the __init__ method, the first argument is always self:
class Dog:
# Class Attribute
species = 'mammal'
# instance method
def description(self):
return "{} is {} years old".format(self.name, self.age)
# instance method
def speak(self, sound):
return "{} says {}".format(self.name, sound)
In the latter method, speak(), we are defining behavior. What other behaviors
could you assign to a dog? Look back to the beginning paragraph to see some
example behaviors for other objects.
Modifying Attributes
You can change the value of attributes based on some behavior:
>>>
>>> class Email:
... def __init__(self):
... self.is_sent = False
... def send_email(self):
... self.is_sent = True
...
>>> my_email = Email()
>>> my_email.is_sent
False
6 of 16 11/26/18, 1:20 AM
Object-Oriented Programming (OOP) in Python 3 – Real Python https://realpython.com/python3-object-oriented-programming/
>>> my_email.send_email()
>>> my_email.is_sent
True
Here, we added a method to send an email, which updates the is_sent variable to
True.
It’s important to note that child classes override or extend the functionality (e.g.,
attributes and behaviors) of parent classes. In other words, child classes inherit all
of the parent’s attributes and behaviors but can also specify different behavior to
follow. The most basic type of class is an object, which generally all other classes
inherit as their parent.
When you define a new class, Python 3 it implicitly uses object as the parent
class. So the following two definitions are equivalent:
class Dog(object):
pass
class Dog:
pass
What’s another way to differentiate one dog from another? How about the dog’s
breed:
>>>
>>> class Dog:
... def __init__(self, breed):
... self.breed = breed
...
>>> spencer = Dog("German Shepard")
>>> spencer.breed
'German Shepard'
>>> sara = Dog("Boston Terrier")
7 of 16 11/26/18, 1:20 AM
Object-Oriented Programming (OOP) in Python 3 – Real Python https://realpython.com/python3-object-oriented-programming/
>>> sara.breed
'Boston Terrier'
Each breed of dog has slightly different behaviors. To take these into account, let’s
create separate classes for each breed. These are child classes of the parent Dog
class.
# Class attribute
species = 'mammal'
# instance method
def description(self):
return "{} is {} years old".format(self.name, self.age)
# instance method
def speak(self, sound):
return "{} says {}".format(self.name, sound)
Read the comments aloud as you work through this program to help you
understand what’s happening, then before you run the program, see if you can
predict the expected output.
8 of 16 11/26/18, 1:20 AM
Object-Oriented Programming (OOP) in Python 3 – Real Python https://realpython.com/python3-object-oriented-programming/
# Class attribute
species = 'mammal'
# instance method
def description(self):
return "{} is {} years old".format(self.name, self.age)
# instance method
def speak(self, sound):
return "{} says {}".format(self.name, sound)
9 of 16 11/26/18, 1:20 AM
Object-Oriented Programming (OOP) in Python 3 – Real Python https://realpython.com/python3-object-oriented-programming/
print(isinstance(johnnywalker, Bulldog))
Output:
>>>
('Jim', 12)
Jim runs slowly
True
True
False
Traceback (most recent call last):
File "dog_isinstance.py", line 50, in <module>
print(isinstance(julie, jim))
TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types
Make sense? Both jim and julie are instances of the Dog() class, while
johnnywalker is not an instance of the Bulldog() class. Then as a sanity check, we
tested if julie is an instance of jim, which is impossible since jim is an instance
of a class rather than a class itself—hence the reason for the TypeError.
>>>
>>> class Dog:
... species = 'mammal'
...
>>> class SomeBreed(Dog):
... pass
...
>>> class SomeOtherBreed(Dog):
... species = 'reptile'
...
>>> frank = SomeBreed()
>>> frank.species
'mammal'
>>> beans = SomeOtherBreed()
>>> beans.species
'reptile'
The SomeBreed() class inherits the species from the parent class, while the
SomeOtherBreed() class overrides the species, setting it to reptile.
10 of 16 11/26/18, 1:20 AM
Object-Oriented Programming (OOP) in Python 3 – Real Python https://realpython.com/python3-object-oriented-programming/
Tom is 6.
Fletcher is 7.
Larry is 9.
And they're all mammals, of course.
Starter code:
# Parent class
class Dog:
# Class attribute
species = 'mammal'
# instance method
def description(self):
return "{} is {} years old".format(self.name, self.age)
# instance method
def speak(self, sound):
return "{} says {}".format(self.name, sound)
# Parent class
class Pets:
dogs = []
# Parent class
class Dog:
# Class attribute
species = 'mammal'
# Instance method
def description(self):
return self.name, self.age
# Instance method
def speak(self, sound):
return "%s says %s" % (self.name, sound)
# Instance method
11 of 16 11/26/18, 1:20 AM
Object-Oriented Programming (OOP) in Python 3 – Real Python https://realpython.com/python3-object-oriented-programming/
def eat(self):
self.is_hungry = False
# Output
print("I have {} dogs.".format(len(my_pets.dogs)))
for dog in my_pets.dogs:
print("{} is {}.".format(dog.name, dog.age))
Using the same file, add an instance attribute of is_hungry = True to the Dog class.
Then add a method called eat() which changes the value of is_hungry to False
when called. Figure out the best way to feed each dog and then output “My dogs
are hungry.” if all are hungry or “My dogs are not hungry.” if all are not hungry.
The final output should look like this:
I have 3 dogs.
Tom is 6.
Fletcher is 7.
Larry is 9.
And they're all mammals, of course.
My dogs are not hungry.
# Parent class
class Pets:
dogs = []
# Parent class
class Dog:
# Class attribute
species = 'mammal'
12 of 16 11/26/18, 1:20 AM
Object-Oriented Programming (OOP) in Python 3 – Real Python https://realpython.com/python3-object-oriented-programming/
self.is_hungry = True
# Instance method
def description(self):
return self.name, self.age
# Instance method
def speak(self, sound):
return "%s says %s" % (self.name, sound)
# Instance method
def eat(self):
self.is_hungry = False
# Output
print("I have {} dogs.".format(len(my_pets.dogs)))
for dog in my_pets.dogs:
dog.eat()
print("{} is {}.".format(dog.name, dog.age))
are_my_dogs_hungry = False
for dog in my_pets.dogs:
if dog.is_hungry:
are_my_dogs_hungry = True
if are_my_dogs_hungry:
print("My dogs are hungry.")
else:
print("My dogs are not hungry.")
Next, add a walk() method to both the Pets and Dog classes so that when you call
the method on the Pets class, each dog instance assigned to the Pets class will
walk(). Save this as dog_walking.py. This is slightly more difficult.
Start by implementing the method in the same manner as the speak() method. As
for the method in the Pets class, you will need to iterate through the list of dogs,
then call the method itself.
13 of 16 11/26/18, 1:20 AM
Object-Oriented Programming (OOP) in Python 3 – Real Python https://realpython.com/python3-object-oriented-programming/
Tom is walking!
Fletcher is walking!
Larry is walking!
# Parent class
class Pets:
dogs = []
def walk(self):
for dog in self.dogs:
print(dog.walk())
# Parent class
class Dog:
# Class attribute
species = 'mammal'
is_hungry = True
# Instance method
def description(self):
return self.name, self.age
# Instance method
def speak(self, sound):
return "%s says %s" % (self.name, sound)
# Instance method
def eat(self):
self.is_hungry = False
def walk(self):
return "%s is walking!" % (self.name)
14 of 16 11/26/18, 1:20 AM
Object-Oriented Programming (OOP) in Python 3 – Real Python https://realpython.com/python3-object-oriented-programming/
# Output
my_pets.walk()
Answer the following questions about OOP to check your learning progress:
1. What’s a class?
2. What’s an instance?
3. What’s the relationship between a class and an instance?
4. What’s the Python syntax used for defining a new class?
5. What’s the spelling convention for a class name?
6. How do you instantiate, or create an instance of, a class?
7. How do you access the attributes and behaviors of a class instance?
8. What’s a method?
9. What’s the purpose of self?
10. What’s the purpose of the __init__ method?
11. Describe how inheritance helps prevent code duplication.
12. Can child classes override properties of their parents?
Conclusion
You should now know what classes are, why you would want or need to use them,
and how to create both parent and child classes to better structure your programs.
Please be aware that OOP is a programming paradigm and not a Python concept.
Most of the modern programming languages such as Java, C#, C++ follow OOP
principles. So the good news is that learning object-oriented programming
fundamentals will be valuable to you in a variety of circumstances—whether
you’re working in Python or not.
15 of 16 11/26/18, 1:20 AM
Object-Oriented Programming (OOP) in Python 3 – Real Python https://realpython.com/python3-object-oriented-programming/
Free Bonus: Click here to get access to a free Python OOP Cheat Sheet that points
you to the best tutorials, videos, and books to learn more about Object-Oriented
Programming with Python.
16 of 16 11/26/18, 1:20 AM
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: