Learn Python For Beginners

Object-Oriented Programming In Python

Object-oriented programming in python is a programming paradigm. We know about two other programming paradigms from following the series of lessons for learning python for beginners. These paradigms are known as imperative and functional programming. In imperative programming logic is run using statements loops in python and other approaches. Functional programming is the practice of using purely functions in python to run logic. Object-oriented programming is the practice of creating objects that describe themselves and are reusable. This is done with the use of classes. A common description of a class would be a blueprint. You will use the same class repeatedly to spawn different objects with different attributes. If this concept confuses you, let’s just dive in and explain as we go.

Object-oriented programming in python

If the code above confuses you, don’t worry we will explain everything one by one.

Understanding Object-Oriented Programming In Python

Notice the difference in the naming convention for classes for object-oriented programming In python. Employee is capitalized this time instead of starting lowercase. Although naming your classes this way ensures distinction of classes from functions it isn’t required. Next we see something that resembles the definition of a function inside the class named __init__. Functions inside of classes in python are called methods. We call this specific method the constructor. Basically it should exist in every class you write in python.

The parameters, or attributes for methods, are requirements for every instance of the class we create. The constructor takes the attributes passed in on creation and assign them to that instance of the object. All methods in objects must have self as their first parameter. But you don’t need to pass a value to self when calling methods. Basically, in a method, self refers to the instance calling the method. Let’s learn more about methods in python.

Methods In Object-Oriented Programming In Python

Essentially methods are just functions inside of classes. Generally they behave the exact same way. Methods however perform logic on the individual instances of the class. Therefore, if we wanted to write a method in our employee class to enable us to give an employee a raise, it would look like this:

Object-oriented programming in python

The way we call methods and access attribute with object-oriented programming in python is the dot notation. Before clicking run, try to guess the output in the terminal by working through the logic mentally. When you do click run you should see something like this:

Hopefully you can see how object-oriented programming makes your software easier to write and understand. Before we move on however, let’s talk about inheritance.

Inheritance In Python

Inheritance in python provides a way to share functionality between similar classes that have different methods or attributes. Let’s illustrate this idea now with a popular practice.

object-oriented programming in python

Before running the program, take a look at the code thoroughly. The first class, the animal class, is known as the superclass and the dog and cat classes are known as a subclass. Note how the subclasses do not contain a constructor method. Also, notice how the subclasses don’t contain the attributes from the superclass but still have access to them when we instantiate an object from them. The subclasses do however contain methods that are specific to each of them. Although both cats and dogs are animals, only cats can purr and only dogs can bark. If we were to try to call the bark method from a cat object, we would get an error. Finally, be sure to examine how each subclass has the superclass in parentheses while defining them. This signals to python that we are inheriting the methods and attributes from another class. Click run.

Wrapping Up

Object-oriented programming in python typically confuses people at first. Let me try to answer some questions you may have. The variables that hold the instance of a class would be the object, not the class. The class itself defines how each object made from it will behave. Essentially the variable becomes the object. We can then use the object to access and change its attributes or call its methods. When changing an attribute in an object, we use the self attribute and dot notation. We would say self.attribute = “something”. To change the value of an attribute, the efficient way to do so is write a class method to complete the process. There are many more concepts to cover with object-oriented programming, but what we discussed in the lesson gives you a great understanding of the fundamentals.

Now What?

You just completed the learning python for beginners series of lessons. You know everything you need to know to start making your own python programs. Apply your knowledge to write a piece of software that solves a problem, even if you already have software to do so. Follow some other tutorials online and learn more about the language. Alternatively, head over to github and look at some open-source projects. We will return to python later to learn more advanced ideas. For now, moving on to other languages should be the goal. You will find now that you know one programming language, learning another language will be much easier. You will also find that learning a different language will get you a better understanding of how python works.

Moving Forward

As mentioned before, take some time to experiment with what you know. Once you feel more confident with object-oriented programming and the other topics in these lessons, come back because our next project will be learning how to build a text adventure game in python.

Leave a Comment

Your email address will not be published. Required fields are marked *