Movatterモバイル変換


[0]ホーム

URL:


— FREE Email Series —

🐍 Python Tricks 💌

Python Tricks Dictionary Merge

🔒 No spam. Unsubscribe any time.

Browse TopicsGuided Learning Paths
Basics Intermediate Advanced
apibest-practicescareercommunitydatabasesdata-sciencedata-structuresdata-vizdevopsdjangodockereditorsflaskfront-endgamedevguimachine-learningnumpyprojectspythontestingtoolsweb-devweb-scraping

Table of Contents

Recommended Video Course
Intro to Object-Oriented Programming (OOP) in Python

OOP in Python 3

Object-Oriented Programming (OOP) in Python

byDavid Amos Dec 15, 2024intermediatepython

Table of Contents

Remove ads

Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding:Intro to Object-Oriented Programming (OOP) in Python

Object-oriented programming (OOP) in Python helps you structure your code by grouping related data and behaviors into objects. You start by defining classes, which act as blueprints, and then create objects from them. OOP simplifies modeling real-world concepts in your programs and enables you to build systems that are more reusable and scalable.

By the end of this tutorial, you’ll understand that:

  • Object-oriented programming in Python involves creating classes as blueprints for objects. These objects contain data and the methods needed to manipulate that data.
  • The four key concepts of OOP in Python are encapsulation, inheritance, abstraction, and polymorphism.
  • Youcreate an object in Python byinstantiating a class, which involves calling the class name followed by parentheses.
  • Class inheritance in Python allows a class to inherit attributes and methods from another class, known as the parent class.
  • You use super() in Python to call a method from the parent class, allowing you toextend or modify inherited behavior.

You’ll explore how to define classes, instantiate classes to create objects, and leverage inheritance to build robust systems in Python.

Note: This tutorial is adapted from the chapter “Object-Oriented Programming (OOP)” inPython Basics: A Practical Introduction to Python 3.

The book uses Python’s built-inIDLE editor to create and edit Python files and interact with the Python shell, so you’ll see occasional references to IDLE throughout this tutorial. If you don’t use IDLE, you canrun the example code from the editor and environment of your choice.

Get Your Code:Click here to download the free sample code that shows you how to do object-oriented programming with classes in Python 3.

Take the Quiz: Test your knowledge with our interactive “Object-Oriented Programming (OOP) in Python” quiz. You’ll receive a score upon completion to help you track your learning progress:


OOP in Python 3

Interactive Quiz

Object-Oriented Programming (OOP) in Python

Object-oriented programming (OOP) is a method of structuring a program by bundling related properties and behaviors into individual objects.

What Is Object-Oriented Programming in Python?

Object-oriented programming is aprogramming paradigm that provides a means of structuring programs so that properties and behaviors are bundled into individualobjects.

For example, an object could represent a person withproperties like a name, age, and address andbehaviors such as walking, talking, breathing, and running. Or it could represent anemail with properties like a recipient list, subject, and body and behaviors like adding attachments and sending.

Put another way, object-oriented programming is an approach for modeling concrete, real-world things, like cars, as well as relations between things, like companies and employees or students and teachers. OOP models real-world entities as software objects that have some data associated with them and can perform certain operations.

OOP also exists in other programming languages and is often described to center around the four pillars, orfour tenants of OOP:

  1. Encapsulation allows you to bundle data (attributes) and behaviors (methods) within a class to create a cohesive unit. By defining methods to control access to attributes and its modification, encapsulation helps maintain data integrity and promotes modular, secure code.

  2. Inheritance enables the creation of hierarchical relationships between classes, allowing a subclass to inherit attributes and methods from a parent class. This promotes code reuse and reduces duplication.

  3. Abstraction focuses on hiding implementation details and exposing only the essential functionality of an object. By enforcing a consistent interface, abstraction simplifies interactions with objects, allowing developers to focus on what an object does rather than how it achieves its functionality.

  4. Polymorphism allows you to treat objects of different types as instances of the same base type, as long as they implement a common interface or behavior. Python’sduck typing make it especially suited for polymorphism, as it allows you to access attributes and methods on objects without needing to worry about their actual class.

In this tutorial you’ll take a practical approach to understanding OOP in Python. But keeping these four concepts of object-oriented programming in mind may help you to remember the information that you gather.

The key takeaway is that objects are at the center of object-oriented programming in Python. In other programming paradigms, objects only represent the data. In OOP, they additionally inform the overall structure of the program.

How Do You Define a Class in Python?

In Python, you define a class by using theclass keyword followed by a name and a colon. Then you use.__init__() to declare which attributes each instance of the class should have:

Python
classEmployee:def__init__(self,name,age):self.name=nameself.age=age

But what does all of that mean? And why do you even need classes in the first place? Take a step back and consider using built-in, primitivedata structures as an alternative.

Primitive data structures—likenumbers,strings, andlists—are designed to represent straightforward pieces of information, such as the cost of an apple, the name of a poem, or your favorite colors, respectively. What if you want to represent something more complex?

For example, you might want to track employees in an organization. You need to store some basic information about each employee, such as their name, age, position, and the year they started working.

One way to do this is to represent each employee as alist:

Python
kirk=["James Kirk",34,"Captain",2265]spock=["Spock",35,"Science Officer",2254]mccoy=["Leonard McCoy","Chief Medical Officer",2266]

There are a number of issues with this approach.

First, it can make larger code files more difficult to manage. If you referencekirk[0] several lines away from where you declared thekirk list, will you remember that the element withindex0 is the employee’s name?

Second, it can introduce errors if employees don’t have the same number of elements in their respective lists. In themccoy list above, the age is missing, somccoy[1] will return"Chief Medical Officer" instead ofDr. McCoy’s age.

A great way to make this type of code more manageable and more maintainable is to useclasses.

Classes vs Instances

Classes allow you to create user-defined data structures. Classes define functions calledmethods, which identify the behaviors and actions that an object created from the class can perform with its data.

In this tutorial, you’ll create aDog class that stores some information about the characteristics and behaviors that an individual dog can have.

A class is a blueprint for how to define something. It doesn’t actually contain any data. TheDog class specifies that a name and an age are necessary for defining a dog, but it doesn’t contain the name or age of any specific dog.

While the class is the blueprint, aninstance is an object that’s built from a class and contains real data. An instance of theDog class is not a blueprint anymore. It’s an actual dog with a name, like Miles, who’s four years old.

Put another way, a class is like a form or questionnaire. An instance is like a form that you’ve filled out with information. Just like many people can fill out the same form with their own unique information, you can create many instances from a single class.

Class Definition

You start all class definitions with theclass keyword, then add the name of the class and a colon. Python will consider any code that you indent below the class definition as part of the class’s body.

Here’s an example of aDog class:

Pythondog.py
classDog:pass

The body of theDog class consists of a single statement: thepass keyword. Python programmers often usepass as a placeholder indicating where code will eventually go. It allows you to run this code without Python throwing an error.

Note: Python class names are written inCapitalizedWords notation by convention. For example, a class for a specific breed of dog, like the Jack Russell Terrier, would be written asJackRussellTerrier.

TheDog class isn’t very interesting right now, so you’ll spruce it up a bit by defining some properties that allDog objects should have. There are several properties that you can choose from, including name, age, coat color, and breed. To keep the example small in scope, you’ll just use name and age.

You define the properties that allDog objects must have in a method called.__init__(). Every time you create a newDog object,.__init__() sets the initialstate of the object by assigning the values of the object’s properties. That is,.__init__() initializes each new instance of the class.

You can give.__init__() any number of parameters, but the first parameter will always be avariable calledself. When you create a new class instance, then Python automatically passes the instance to theself parameter in.__init__() so that Python can define the newattributes on the object.

Update theDog class with an.__init__() method that creates.name and.age attributes:

Pythondog.py
classDog:def__init__(self,name,age):self.name=nameself.age=age

Make sure that you indent the.__init__() method’s signature by four spaces, and the body of the method by eight spaces. This indentation is vitally important. It tells Python that the.__init__() method belongs to theDog class.

In the body of.__init__(), there are two statements using theself variable:

  1. self.name = name creates an attribute calledname and assigns the value of thename parameter to it.
  2. self.age = age creates an attribute calledage and assigns the value of theage parameter to it.

Attributes created in.__init__() are calledinstance attributes. An instance attribute’s value is specific to a particular instance of the class. AllDog objects have a name and an age, but the values for thename andage attributes will vary depending on theDog instance.

On the other hand,class attributes are attributes that have the same value for all class instances. You can define a class attribute by assigning a value to avariable name outside of.__init__().

For example, the followingDog class has a class attribute calledspecies with the value"Canis familiaris":

Pythondog.py
classDog:species="Canis familiaris"def__init__(self,name,age):self.name=nameself.age=age

You define class attributes directly beneath the first line of the class name and indent them by four spaces. You always need to assign them an initial value. When you create an instance of the class, then Python automatically creates and assigns class attributes to their initial values.

Use class attributes to define properties that should have the same value for every class instance. Use instance attributes for properties that vary from one instance to another.

Now that you have aDog class, it’s time to create some dogs!

How Do You Instantiate a Class in Python?

Creating a new object from a class is calledinstantiating a class. You can create a new object by typing the name of the class, followed by opening and closing parentheses:

Python
>>>classDog:...pass...>>>Dog()<__main__.Dog object at 0x106702d30>

You first create a newDog class with no attributes or methods, and then you instantiate theDog class to create aDog object.

In the output above, you can see that you now have a newDog object at0x106702d30. This funny-looking string of letters and numbers is amemory address that indicates where Python stores theDog object in your computer’s memory. Note that the address on your screen will be different.

Now instantiate theDog class a second time to create anotherDog object:

Python
>>>Dog()<__main__.Dog object at 0x0004ccc90>

The newDog instance is located at a different memory address. That’s because it’s an entirely new instance and is completely unique from the firstDog object that you created.

To see this another way, type the following:

Python
>>>a=Dog()>>>b=Dog()>>>a==bFalse

In this code, you create two newDog objects and assign them to the variablesa andb. When you comparea andb using the== operator, the result isFalse. Even thougha andb are both instances of theDog class, they represent two distinct objects in memory.

Class and Instance Attributes

Now create a newDog class with a class attribute called.species and two instance attributes called.name and.age:

Python
>>>classDog:...species="Canis familiaris"...def__init__(self,name,age):...self.name=name...self.age=age...

To instantiate thisDog class, you need to provide values forname andage. If you don’t, then Python raises aTypeError:

Python
>>>Dog()Traceback (most recent call last):...TypeError:__init__() missing 2 required positional arguments: 'name' and 'age'

To pass arguments to thename andage parameters, put values into the parentheses after the class name:

Python
>>>miles=Dog("Miles",4)>>>buddy=Dog("Buddy",9)

This creates two newDog instances—one for a four-year-old dog named Miles and one for a nine-year-old dog named Buddy.

TheDog class’s.__init__() method has three parameters, so why are you only passing two arguments to it in the example?

When you instantiate theDog class, Python creates a new instance ofDog and passes it to the first parameter of.__init__(). This essentially removes theself parameter, so you only need to worry about thename andage parameters.

Note: Behind the scenes, Python both creates and initializes a new object when you use this syntax. If you want to dive deeper, then you can read the dedicated tutorial about thePython class constructor.

After you create theDog instances, you can access their instance attributes usingdot notation:

Python
>>>miles.name'Miles'>>>miles.age4>>>buddy.name'Buddy'>>>buddy.age9

You can access class attributes the same way:

Python
>>>buddy.species'Canis familiaris'

One of the biggest advantages of using classes to organize data is that instances are guaranteed to have the attributes you expect. AllDog instances have.species,.name, and.age attributes, so you can use those attributes with confidence, knowing that they’ll always return a value.

Although the attributes are guaranteed to exist, their valuescan change dynamically:

Python
>>>buddy.age=10>>>buddy.age10>>>miles.species="Felis silvestris">>>miles.species'Felis silvestris'

In this example, you change the.age attribute of thebuddy object to10. Then you change the.species attribute of themiles object to"Felis silvestris", which is a species of cat. That makes Miles a pretty strange dog, but it’s valid Python!

The key takeaway here is that custom objects are mutable by default. An object is mutable if you can alter it dynamically. For example, lists anddictionaries are mutable, but strings and tuples areimmutable.

Instance Methods

Instance methods are functions that you define inside a class and can only call on an instance of that class. Just like.__init__(), an instance method always takesself as its first parameter.

Open a new editor window in IDLE and type in the followingDog class:

Pythondog.py
classDog:species="Canis familiaris"def__init__(self,name,age):self.name=nameself.age=age# Instance methoddefdescription(self):returnf"{self.name} is{self.age} years old"# Another instance methoddefspeak(self,sound):returnf"{self.name} says{sound}"

ThisDog class has two instance methods:

  1. .description() returns a string displaying the name and age of the dog.
  2. .speak() has one parameter calledsound and returns a string containing the dog’s name and the sound that the dog makes.

Save the modifiedDog class to a file calleddog.py and pressF5 to run the program. Then open the interactive window and type the following to see your instance methods in action:

Python
>>>miles=Dog("Miles",4)>>>miles.description()'Miles is 4 years old'>>>miles.speak("Woof Woof")'Miles says Woof Woof'>>>miles.speak("Bow Wow")'Miles says Bow Wow'

In the aboveDog class,.description() returns a string containing information about theDog instancemiles. When writing your own classes, it’s a good idea to have a method that returns a string containing useful information about an instance of the class. However,.description() isn’t the mostPythonic way of doing this.

When you create alist object, you can useprint() to display a string that looks like the list:

Python
>>>names=["Miles","Buddy","Jack"]>>>print(names)['Miles', 'Buddy', 'Jack']

Go ahead and print themiles object to see what output you get:

Python
>>>print(miles)<__main__.Dog object at 0x00aeff70>

When you printmiles, you get a cryptic-looking message telling you thatmiles is aDog object at the memory address0x00aeff70. This message isn’t very helpful. You can change what gets printed by defining a special instance method called.__str__().

In the editor window, change the name of theDog class’s.description() method to.__str__():

Pythondog.py
classDog:# ...def__str__(self):returnf"{self.name} is{self.age} years old"

Save the file and pressF5. Now, when you printmiles, you get a much friendlier output:

Python
>>>miles=Dog("Miles",4)>>>print(miles)'Miles is 4 years old'

Methods like.__init__() and.__str__() are calleddunder methods because they begin and end withdouble underscores. There are many dunder methods that you can use to customize classes in Python.Understanding dunder methods is an important part of mastering object-oriented programming in Python, but for your first exploration of the topic, you’ll stick with these two dunder methods.

Note: Check outWhen Should You Use.__repr__() vs.__str__() in Python? to learn more about.__str__() and its cousin.__repr__().

If you want to reinforce your understanding with a practical exercise, then you can click on the block below and work on solving the challenge:

Create aCar class with two instance attributes:

  1. .color, which stores the name of the car’s color as a string
  2. .mileage, which stores the number of miles on the car as an integer

Then create twoCar objects—a blue car with twenty thousand miles and a red car with thirty thousand miles—and print out their colors and mileage. Your output should look like this:

Shell
The blue car has 20,000 milesThe red car has 30,000 miles

There are multiple ways to solve this challenge. To effectively practice what you’ve learned so far, try to solve the task with the information about classes in Python that you’ve gathered in this section.

When you’re done with your own implementation of the challenge, then you can expand the block below to see a possible solution:

First, create aCar class with.color and.mileage instance attributes, and a.__str__() method to format the display of objects when you pass them toprint():

Python
>>>classCar:...def__init__(self,color,mileage):...self.color=color...self.mileage=mileage...def__str__(self):...returnf"The{self.color} car has{self.mileage:,} miles"...

Thecolor andmileage parameters of.__init__() are assigned toself.color andself.mileage, which creates the two instance attributes.

The.__str__() method interpolates both instance attributes into anf-string and uses the:,format specifier to print the mileage grouped by thousands and separated with a comma.

Now you can create the twoCar instances:

Python
>>>blue_car=Car(color="blue",mileage=20_000)>>>red_car=Car(color="red",mileage=30_000)

You create theblue_car instance by passing the value"blue" to thecolor parameter and20_000 to themileage parameter. Similarly, you createred_car with the values"red" and30_000.

To print the color and mileage of eachCar object, you can loop over atuple containing both objects and print each object:

Python
>>>forcarin(blue_car,red_car):...print(car)...The blue car has 20,000 milesThe red car has 30,000 miles

Because you’ve defined their string representation in.__str__(), printing the objects gives you the desired text output.

When you’re ready, you can move on to the next section. There, you’ll see how to take your knowledge one step further and create classes from other classes.

How Do You Inherit From Another Class in Python?

Inheritance is the process by which one class takes on the attributes and methods of another. Newly formed classes are calledchild classes, and the classes that you derive child classes from are calledparent classes.

You inherit from a parent class by creating a new class and putting the name of the parent class into parentheses:

Pythoninheritance.py
classParent:hair_color="brown"classChild(Parent):pass

In this minimal example, the child classChild inherits from the parent classParent. Because child classes take on the attributes and methods of parent classes,Child.hair_color is also"brown" without your explicitly defining that.

Note: This tutorial is adapted from the chapter “Object-Oriented Programming (OOP)” inPython Basics: A Practical Introduction to Python 3. If you enjoy what you’re reading, then be sure to check outthe rest of the book and thelearning path.

You can also check out thePython Basics: Building Systems With Classes video course to reinforce the skills that you’ll develop in this section of the tutorial.

Child classes can override or extend the attributes and methods of parent classes. In other words, child classes inherit all of the parent’s attributes and methods but can also specify attributes and methods that are unique to themselves.

Although the analogy isn’t perfect, you can think of object inheritance sort of like genetic inheritance.

You may have inherited your hair color from your parents. It’s an attribute that you were born with. But maybe you decide to color your hair purple. Assuming that your parents don’t have purple hair, you’ve justoverridden the hair color attribute that you inherited from your parents:

Pythoninheritance.py
classParent:hair_color="brown"classChild(Parent):hair_color="purple"

If you change the code example like this, thenChild.hair_color will be"purple".

You also inherit, in a sense, your language from your parents. If your parents speak English, then you’ll also speak English. Now imagine you decide to learn a second language, like German. In this case, you’veextended your attributes because you’ve added an attribute that your parents don’t have:

Pythoninheritance.py
classParent:speaks=["English"]classChild(Parent):def__init__(self):super().__init__()self.speaks.append("German")

You’ll learn more about how the code above works in the sections below. But before you dive deeper into inheritance in Python, you’ll take a walk to a dog park to better understand why you might want to use inheritance in your own code.

Example: Dog Park

Pretend for a moment that you’re at a dog park. There are many dogs of different breeds at the park, all engaging in various dog behaviors.

Suppose now that you want to model the dog park with Python classes. TheDog class that you wrote in the previous section can distinguish dogs by name and age but not by breed.

You could modify theDog class in the editor window by adding a.breed attribute:

Pythondog.py
classDog:species="Canis familiaris"def__init__(self,name,age,breed):self.name=nameself.age=ageself.breed=breeddef__str__(self):returnf"{self.name} is{self.age} years old"defspeak(self,sound):returnf"{self.name} says{sound}"

PressF5 to save the file. Now you can model the dog park by creating a bunch of different dogs in the interactive window:

Python
>>>miles=Dog("Miles",4,"Jack Russell Terrier")>>>buddy=Dog("Buddy",9,"Dachshund")>>>jack=Dog("Jack",3,"Bulldog")>>>jim=Dog("Jim",5,"Bulldog")

Each breed of dog has slightly different behaviors. For example, bulldogs have a low bark that sounds likewoof, but dachshunds have a higher-pitched bark that sounds more likeyap.

Using just theDog class, you must supply a string for thesound argument of.speak() every time you call it on aDog instance:

Python
>>>buddy.speak("Yap")'Buddy says Yap'>>>jim.speak("Woof")'Jim says Woof'>>>jack.speak("Woof")'Jack says Woof'

Passing a string to every call to.speak() is repetitive and inconvenient. Moreover, the.breed attribute should determine the string representing the sound that eachDog instance makes, but here you have to manually pass the correct string to.speak() every time you call it.

You can simplify the experience of working with theDog class by creating a child class for each breed of dog. This allows you to extend the functionality that each child class inherits, including specifying a default argument for.speak().

Parent Classes vs Child Classes

In this section, you’ll create a child class for each of the three breeds mentioned above: Jack Russell terrier, dachshund, and bulldog.

For reference, here’s the full definition of theDog class that you’re currently working with:

Pythondog.py
classDog:species="Canis familiaris"def__init__(self,name,age):self.name=nameself.age=agedef__str__(self):returnf"{self.name} is{self.age} years old"defspeak(self,sound):returnf"{self.name} says{sound}"

After doing thedog park example in the previous section, you’ve removed.breed again. You’ll now write code to keep track of a dog’s breed using child classes instead.

To create a child class, you create a new class with its own name and then put the name of the parent class in parentheses. Add the following to thedog.py file to create three new child classes of theDog class:

Pythondog.py
# ...classJackRussellTerrier(Dog):passclassDachshund(Dog):passclassBulldog(Dog):pass

PressF5 to save and run the file. With the child classes defined, you can now create some dogs of specific breeds in the interactive window:

Python
>>>miles=JackRussellTerrier("Miles",4)>>>buddy=Dachshund("Buddy",9)>>>jack=Bulldog("Jack",3)>>>jim=Bulldog("Jim",5)

Instances of child classes inherit all of the attributes and methods of the parent class:

Python
>>>miles.species'Canis familiaris'>>>buddy.name'Buddy'>>>print(jack)Jack is 3 years old>>>jim.speak("Woof")'Jim says Woof'

To determine which class a given object belongs to, you can use the built-intype():

Python
>>>type(miles)<class '__main__.JackRussellTerrier'>

What if you want to determine ifmiles is also an instance of theDog class? You can do this with the built-inisinstance():

Python
>>>isinstance(miles,Dog)True

Notice thatisinstance() takes two arguments, an object and a class. In the example above,isinstance() checks ifmiles is an instance of theDog class and returnsTrue.

Themiles,buddy,jack, andjim objects are allDog instances, butmiles isn’t aBulldog instance, andjack isn’t aDachshund instance:

Python
>>>isinstance(miles,Bulldog)False>>>isinstance(jack,Dachshund)False

More generally, all objects created from a child class are instances of the parent class, although they may not be instances of other child classes.

Now that you’ve created child classes for some different breeds of dogs, you can give each breed its own sound.

Parent Class Functionality Extension

Since different breeds of dogs have slightly different barks, you want to provide a default value for thesound argument of their respective.speak() methods. To do this, you need to override.speak() in the class definition for each breed.

To override a method defined on the parent class, you define a method with the same name on the child class. Here’s what that looks like for theJackRussellTerrier class:

Pythondog.py
# ...classJackRussellTerrier(Dog):defspeak(self,sound="Arf"):returnf"{self.name} says{sound}"# ...

Now.speak() is defined on theJackRussellTerrier class with the default argument forsound set to"Arf".

Updatedog.py with the newJackRussellTerrier class and pressF5 to save and run the file. You can now call.speak() on aJackRussellTerrier instance without passing an argument tosound:

Python
>>>miles=JackRussellTerrier("Miles",4)>>>miles.speak()'Miles says Arf'

Sometimes dogs make different noises, so if Miles gets angry and growls, you can still call.speak() with a different sound:

Python
>>>miles.speak("Grrr")'Miles says Grrr'

One thing to keep in mind about class inheritance is that changes to the parent class automatically propagate to child classes. This occurs as long as the attribute or method being changed isn’t overridden in the child class.

For example, in the editor window, change the string returned by.speak() in theDog class:

Pythondog.py
classDog:# ...defspeak(self,sound):returnf"{self.name} barks:{sound}"# ...

Save the file and pressF5. Now, when you create a newBulldog instance namedjim,jim.speak() returns the new string:

Python
>>>jim=Bulldog("Jim",5)>>>jim.speak("Woof")'Jim barks: Woof'

However, calling.speak() on aJackRussellTerrier instance won’t show the new style of output:

Python
>>>miles=JackRussellTerrier("Miles",4)>>>miles.speak()'Miles says Arf'

Sometimes it makes sense to completely override a method from a parent class. But in this case, you don’t want theJackRussellTerrier class to lose any changes that you might make to the formatting of theDog.speak() output string.

To do this, you still need to define a.speak() method on the childJackRussellTerrier class. But instead of explicitly defining the output string, you need to call theDog class’s.speak() frominside the child class’s.speak() using the same arguments that you passed toJackRussellTerrier.speak().

You can access the parent class from inside a method of a child class by usingsuper():

Pythondog.py
# ...classJackRussellTerrier(Dog):defspeak(self,sound="Arf"):returnsuper().speak(sound)# ...

When you callsuper().speak(sound) insideJackRussellTerrier, Python searches the parent class,Dog, for a.speak() method and calls it with the variablesound.

Updatedog.py with the newJackRussellTerrier class. Save the file and pressF5 so you can test it in the interactive window:

Python
>>>miles=JackRussellTerrier("Miles",4)>>>miles.speak()'Miles barks: Arf'

Now when you callmiles.speak(), you’ll see output reflecting the new formatting in theDog class.

Note: In the above examples, theclass hierarchy is very straightforward. TheJackRussellTerrier class has a single parent class,Dog. In real-world examples, the class hierarchy can get quite complicated.

Thesuper() function does much more than just search the parent class for a method or an attribute. It traverses the entire class hierarchy for a matching method or attribute. If you aren’t careful,super() can have surprising results.

If you want to check your understanding of the concepts that you learned about in this section with a practical exercise, then you can click on the block below and work on solving the challenge:

Start with the following code for your parentDog class:

Pythondog.py
classDog:species="Canis familiaris"def__init__(self,name,age):self.name=nameself.age=agedef__str__(self):returnf"{self.name} is{self.age} years old"defspeak(self,sound):returnf"{self.name} says{sound}"

Create aGoldenRetriever class that inherits from theDog class. Give thesound argument ofGoldenRetriever.speak() a default value of"Bark".

When you’re done with your own implementation of the challenge, then you can expand the block below to see a possible solution:

Create a class calledGoldenRetriever that inherits from theDog class and overrides the.speak() method:

Pythondog.py
# ...classGoldenRetriever(Dog):defspeak(self,sound="Bark"):returnsuper().speak(sound)

You give"Bark" as the default value to thesound parameter inGoldenRetriever.speak(). Then you usesuper() to call the.speak() method of the parent class with the same argument passed tosound as theGoldenRetriever class’s.speak() method.

Nice work! In this section, you’ve learned how to override and extend methods from a parent class, and you worked on a small practical example to cement your new skills.

Conclusion

In this tutorial, you learned about object-oriented programming (OOP) in Python. Many modern programming languages, such asJava,C#, andC++, follow OOP principles, so the knowledge that you gained here will be applicable no matter where your programming career takes you.

In this tutorial, you learned how to:

  • Define aclass, which is a sort of blueprint for an object
  • Instantiate a class to create anobject
  • Useattributes andmethods to define theproperties andbehaviors of an object
  • Useinheritance to createchild classes from aparent class
  • Reference a method on a parent class usingsuper()
  • Check if an object inherits from another class usingisinstance()

If you enjoyed what you learned in this sample fromPython Basics: A Practical Introduction to Python 3, then be sure to check outthe rest of the book and take a look at ourIntroduction to Python learning path.

Take the Quiz: Test your knowledge with our interactive “Object-Oriented Programming (OOP) in Python” quiz. You’ll receive a score upon completion to help you track your learning progress:


OOP in Python 3

Interactive Quiz

Object-Oriented Programming (OOP) in Python

Object-oriented programming (OOP) is a method of structuring a program by bundling related properties and behaviors into individual objects.

Frequently Asked Questions

Now that you have some experience with object-oriented programming in Python, you can use the questions and answers below to check your understanding and recap what you’ve learned.

These FAQs are related to the most important concepts you’ve covered in this tutorial. Click theShow/Hide toggle beside each question to reveal the answer.

Object-oriented programming in Python is a programming paradigm that structures programs by bundling related properties and behaviors into individual objects, allowing you to model real-world entities with properties and behaviors.

The four pillars of object-oriented programming in Python are encapsulation, inheritance, abstraction, and polymorphism. These principles help you organize code into objects that bundle data and functionality together.

You define a class in Python using theclass keyword followed by the class name and a colon. Then you write an.__init__() method to define the attributes each instance should have.

Class inheritance allows you to create a new child class that inherits attributes and methods from an existing parent class. This enables code reuse and the extension or modification of existing functionality from the parent class.

You instantiate a class in Python by calling the class name followed by parentheses, optionally passing any required arguments to create an instance of the class.

Class attributes are shared across all instances of a class, while instance attributes are unique to each instance, allowing individual objects to have their own attribute values.

Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding:Intro to Object-Oriented Programming (OOP) in Python

🐍 Python Tricks 💌

Get a short & sweetPython Trick delivered to your inbox every couple of days. No spam ever. Unsubscribe any time. Curated by the Real Python team.

Python Tricks Dictionary Merge

AboutDavid Amos

David is a writer, programmer, and mathematician passionate about exploring mathematics through code.

» More about David

Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

MasterReal-World Python Skills With Unlimited Access to Real Python

Locked learning resources

Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:

Level Up Your Python Skills »

MasterReal-World Python Skills
With Unlimited Access to Real Python

Locked learning resources

Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:

Level Up Your Python Skills »

What Do You Think?

Rate this article:

What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment below and let us know.

Commenting Tips: The most useful comments are those written with the goal of learning from or helping out other students.Get tips for asking good questions andget answers to common questions in our support portal.


Looking for a real-time conversation? Visit theReal Python Community Chat or join the next“Office Hours” Live Q&A Session. Happy Pythoning!

Keep Learning

Related Topics:intermediatepython

Recommended Video Course:Intro to Object-Oriented Programming (OOP) in Python

Related Tutorials:

Keep reading Real Python by creating a free account or signing in:

Already have an account?Sign-In

Almost there! Complete this form and click the button below to gain instant access:

OOP in Python 3

Object-Oriented Programming in Python 3 (Sample Code)

🔒 No spam. We take your privacy seriously.


[8]ページ先頭

©2009-2025 Movatter.jp