Python: Advanced OOP

Brandon Thompson
6 min readMar 7, 2017

--

If you are continuing on from my last post, then you have learned some of the basics to Python, such as variables, the declaration and initialization of them, and method creation and calls. If not, then I recommend going to the bottom of this page and clicking the link to the previous blog.

As a review of the previous blog, we used variables and their different types to get different results, such as concatenating Strings and completing mathematical statements. This can all be combined in one or many methods to simply call upon and give a few values. When we look into Object Oriented Programming (OOP) we have to think about giving different modules these specific methods that it alone can do. This system’s definition as:

…a programming paradigm based on the concept of “objects”, which may contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods. -Wikipedia

This is a very common structure of programming when creating desktop applications and even applies to other systems, such as web development and databases. To get a better idea of this, we can think of real life examples that can actually relate to programming. With a car, you have a radio. This radio has many options, such as volume, tuning for frequency, a clock and settings. The object would be the radio, while everything else would be methods and variables.

To get started, we will create a simple object. Create a new Project and give it a name like “AnimalClassList”. We can make a cat first, with an age, a colour, and most importantly, a name. You can choose any data types for them and names to reference them. As you can see, we jump right into a mess here. We will go step by step through the process.

“Cat” class, in a “Cat” module!

For starters, we have to have a module, so create one. Next, we define this as a class by saying class ClassName. You can make the class name different than the module name, but it makes reading through your list of modules easier when you have consistency like this. Also, notice that it is very similar to creating a method. After that, you have an option to create a class description with single quotes. Finally, you need a specialized method called a Constructor. What this method does is create an object of this structure when it is called elsewhere. Rules to this constructor include:

  • Signature must be __init__(self, paramNames, …):
  • If variables associate with the class type, define as self.varName = dataType
  • Only one can be made (can have multiple in other languages)

This is the basics to creating a class, and is quite handy to learn for any object structured program. But moving on, we see I wrote many, many other methods already, so step-by-step, we can explain it all.

For starters, there is a very distinct separation between all these method names, but they have some pretty key words for describing what they do. “Setters”, or methods that start with the word “set”, are methods to set variables in the class. Of course you can name it whatever, but again, consistency and easier to find. The other type is the “Getter” or the method with the word “get”. This gets the value of a variable in the class and sends it back to where it was called using the keyword return.

Wow, that’s a lot, but it wasn’t that complicated. What if this was a radio? We could create a class of Radio, in the constructor we define variables such as volume, frequency, true and false settings, and clock variables. Object Oriented Programming is a good starting point when learning languages, and is also an effective structure, because the basics for an object in real life easily define the blueprint you enter. Of course, it isn’t this simple when you get larger systems, but you get an idea as to where it all goes.

Next, we will cover creating objects and calling their methods! For this, we will be using the Cat Class we just created! First thing’s first, we need to tell another module we are using this object here, otherwise it doesn’t understand what we are doing! As you can see, we need to do what is called importing a class, where we tell it the package name and what we are importing. Next, create a variable name, and assign a value like you would any data type! The only difference here is we have to tell it what the data type is, in this case an Object of Cat from the Cat module. The first value in Cat.Cat() is the module, and the next one is the Class definition.

One more thing, or we get this exception here. We need to define values, which can be done when we create the cat (as shown below).

Of course we use the same steps to give these cats new names, ages, and colours, but we shouldn’t do that, because we wouldn’t do that in real life, so we would actually not use Setters with this class.

Oh well, might as well try!

It can be a useful tool, implementing Setters in classes that need them, such as a radio, where the Object is always changing.

One other component to learn is inheriting classes! Maybe we have a good, basic Class, but we want more specific classes with similar variables. We can achieve this the same way we created a class, except with a value inside the brackets where we first name the class, like shown below. Also notice we have nothing in the class.

If we make a variable of the class and use the same methods as the original class…

Take a moment because that is something special right there.

With this, you can add new methods to the new class, create multiple inherit classes, create unique methods for specific classes, and so on and so forth. This is the power of Object Oriented Programming, as we can create an object to do everything we want, instead of repeating code again and again, and losing data in places an object can hold it.

--

--