Click to share! ⬇️

Object-Oriented Python

Object-Oriented Principles can apply to most computer languages, not just Python. Python does not require the programmer to use objects or classes when creating programs, even though everything within Python itself is an object. It is common to simply create individual functions, variables, data structures, and so on, and Python will run your code as long as it’s syntactically correct. Some of the reasons why programmers make use of OOP, however, is that it becomes more and more difficult to keep everything organized and to ensure that one part of your program isn’t having unintended side effects somewhere else. Using object-oriented programming techniques helps organize and structure programs making them easier to plan, maintain, and learn. OOP groups together data and the functions that operate on that data into one place. It also encourages the building of modular programs, meaning that individual parts of the program don’t need to know how other parts work internally. In the future, if part of the program needs to be updated or replaced, it can be done with minimal disturbance to the code in the rest of the program.


Basic Class Definition

class Monitor():
    def __init__(self, model):
        self.model = model

The class keyword is used to define a new class in Python. Here we create a class called Monitor. You’ll also notice the __init__ function on the second line. It takes two arguments of self and model. Inside the function, model is assigned to self.model. So what is happening here? Well, the __init__ function is one of Python’s special functions for working with classes. It is used to initialize a new object with some starting information and is called before any other functions that you’ve defined on the class. It is similar in nature to a constructor function that you find in Java or C#, but it is not exactly the same. It is specifically an initializer function since the object has already been constructed when it is called.


Create Instances Of The Class

monitor1 = Monitor("Samsung")
monitor2 = Monitor("Viewsonic")

Now we can instantiate a couple of objects from the Monitor class that we have defined. To instantiate an object, we begin with a variable on the left-hand side of an assignment(=) operator. On the right-hand side of the = operator we call the class by writing it’s name, followed by parentheses. Inside of the parentheses we need to pass in a model since the class expects one. What is interesting is that the __init__ function takes two arguments, but we only pass one argument when creating each instance here. That is because whenever a method is called on a Python object, the object itself gets automatically passed in as the first argument. It’s a little bit like magic, but that is how it works. In Python, the convention is to use the self keyword for this. In other languages, this is often the this keyword.


Print The Class and Property

In the section just above, there are now two variables in the program. Each holds an instance of the Monitor class. In Python, to view an object you can simply use the print() function while passing in the object that you would like to view. So here we call print() and pass in the monitor1 variable. That output results in the information that it is a Monitor object at a given address. Then we print out a property, or attribute, of that object. Recall we had assigned a model attribute to the class. Since we passed in the string of “Samsung” when the object was initialized, that is what we see when printing out that property of the object. Neat!

print(monitor1)
print(monitor1.model)

output:

<__main__.Monitor object at 0x01702E98>
Samsung

Following that same process, we print out the second object. Again we see that it is a Monitor object at a given address. Notice that the address number is different than the first one. That shows that each object is its own entity. You can create one, or many objects from a given class and they will all be unique to themselves. We again print out the property on this object and see that this Monitor instance has a model type of Viewsonic, just like we expect.

print(monitor2)
print(monitor2.model)

output:

<__main__.Monitor object at 0x01042F28>
Viewsonic

Next up, we’ll look at instance methods and attributes.

Click to share! ⬇️