Python: Object-Oriented Programming Part 2

Let’s cover inheritance, static/class methods, encapsulationPreviously we spoke about what classes and objects were, how they’re used and why we should use them. This article will cover a few more concepts that are important when working with classes l…


This content originally appeared on Level Up Coding - Medium and was authored by Richard Amin

Let’s cover inheritance, static/class methods, encapsulation

Previously we spoke about what classes and objects were, how they’re used and why we should use them. This article will cover a few more concepts that are important when working with classes like inheritance, property decorators, static/class methods, super class, and private/public methods.

Image by Dylan Gillis

Table of Contents:

  • Inheritance/SuperClass
  • Property, Static and Class Methods
  • Encapsulation
  • Magic/Dunder Methods
  • Conclusion

Inheritance and Super Class:

So now that we covered how to create and setup a class, __init__, attributes, and methods,

Image by Author

lets talk about inheritance. What if we want to create a new class that helps our monster class get some stats so it can wreak havoc. Now is a great time to implement a sub class. The idea is that it can inherit all the attributes and methods from the parent class, Monster, and have them be accessible in our Monster_Stats sub class. Now when we create our Monster_Class object, all the attributes needed to define our Monster’s background information(name, species, height, weight and location) will be initialized by the constructor of our Monster class.

Image by Author

— First we import our Monster class from our create_monster.py module.

— Here we’re defining our sub class just like any normal class, with a few extra additions. We’re still adding the attributes from our parent Monster class in the parameters for the Monster_Stats sub class.

— Now when we create our new Monster objects with it’s stats, it’ll use our Monster parent class’ init to initialize name, species, height, weight, and location.

— So in essence once we create our Monster object using the Monster class, then we give that Monster object stats using our MonsterStats class.

Property, Static Methods and Class Methods:

Property Methods:
Property decorators are denoted with a @property on top of the method in your class. It allows you to access these methods as if they were class attributes.

Image by Author

— Here we have our monster_stats method in our MonsterSuper class which displays the attack and defense power.

— If we were to print this without the @property decorator then we would’ve had to write this call like, print(obelisk.monster_stats()) since it’s declared a method but adding the @property decorator allows us to access this method like a class attribute instead.

Static Methods:
Static methods are denoted with @staticmethod decorator, on top of the designated method. They are bound to the class and not the instance object, nor does it require an instance object to be used. Static methods can be accessed through the class itself. However they are limited since they’re unable to access the class attributes of that class, or class objects of that class because we don’t have self or access to those class properties. In simpler terms, the static method knows nothing about the class that it’s inside of and uses only the parameters given to it. Think of it like a normal python function, just inside a class. To call it, you must use your class name first then the static method itself.

Image by Author

— Here we have our MonsterSuper class where we determine the status of our monster.

— Inside our MonsterSuper class we have our monster_super_tier static method. This method is used to determine if our monsters are in the “super tier” based on their attack power. Not the most elaborate example but bare with me. Here we use Obelisk’s attack power as the input and therefore get a return that he is indeed on the super monster tier level.

— We’re calling the monster_super_tier static method using the class MonsterSuper instead of our class object Obelisk.

Class Methods(cls):
Class methods denoted with a @classmethod decorator, on the top of the designated method allows you to update the default value of one of your class variables across all your objects. Just like how a instance method receives an instance as the initial argument, a class method receives a class as the first argument. An example of this would be, if we wanted to keep count of the amount of super monsters we had as they’re created. We can use the class and class objects to access these methods.

Image by Author

— Here the super_monster_count is a variable that belongs to the MonsterSuper class.

— In our __init__ method we’re calling the up_count method to increment the count as we create our new monster object.

— We call the class method get_count_num three times. The first time we call the class itself to access this class method. The second and third time we use our obelisk and slifer class objects to access the class method. Therefore we have 0, 1, 2 which respectively represents MonsterSuper class, obelisk, and slifer.

Encapsulation:

Allows you to put restrictions on your class attributes/methods to prevent modification of the data. The way it prevents change is by only allowing the objects variable to be changed by class method alone. Meaning that the attribute can’t be changed by altering the value outside of the class like how you’d normally update a object’s attribute.

Private Members:
Private members are private attributes and/or methods that can only be accessed and updated inside the class. These variables are advised to not be used outside of class operations. This prevents any accidental modification of data that you don’t want to be modified, also serves as a great way to hide the inner functionalities of your class that you don’t want exposed. You can denote which methods should be private within a class by using the _before declaring the method name like so, __methodname (make sure to use double underscore before the method name).

Image by Author

— Here we’re trying to access the private monster method in our class, but Python is throwing an error because our __privatemonster method is a private method.

Public Members:
Represents the common attribute and method structure used in normal classes, where they can also be accessed outside the class.

Magic/Dunder Methods:

Are methods with underscores on both ends of method name, like our __init__ method. Dunder stands for double underscores, as you can see. Usually used to overload operators within your class.

Image by Author

— As you can see, when we print out the object without our __repr__ function being initialized in our class, python only prints the object’s location in memory.

Image by Author

— When printing our function with our __repr__ function initialized, we only are getting an actual readable return based on how our __repr__ function is built. In this case getting the Monster name and it’s attack power.

Conclusion:

Using sub classes, encapsulation, static/class and private/public methods adds additional layers of complexity, however allows the user/team to create production level code that can be scaled much more efficiently. Yes the same application maybe developed with a functional programming approach however it’ll increase development time, look much more cluttered, less organized and add much more lines of code to your application. I know I used Yugioh as bit of an inspiration for my examples, but I wanted to make things a bit more fun and as a tribute to creator Kazuki Takahashi, RIP. Hope you guys enjoyed the content. Please clap, share, comment, follow and happy coding!

Related Content:


Python: Object-Oriented Programming Part 2 was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Richard Amin


Print Share Comment Cite Upload Translate Updates
APA

Richard Amin | Sciencx (2022-10-18T02:10:57+00:00) Python: Object-Oriented Programming Part 2. Retrieved from https://www.scien.cx/2022/10/18/python-object-oriented-programming-part-2/

MLA
" » Python: Object-Oriented Programming Part 2." Richard Amin | Sciencx - Tuesday October 18, 2022, https://www.scien.cx/2022/10/18/python-object-oriented-programming-part-2/
HARVARD
Richard Amin | Sciencx Tuesday October 18, 2022 » Python: Object-Oriented Programming Part 2., viewed ,<https://www.scien.cx/2022/10/18/python-object-oriented-programming-part-2/>
VANCOUVER
Richard Amin | Sciencx - » Python: Object-Oriented Programming Part 2. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/10/18/python-object-oriented-programming-part-2/
CHICAGO
" » Python: Object-Oriented Programming Part 2." Richard Amin | Sciencx - Accessed . https://www.scien.cx/2022/10/18/python-object-oriented-programming-part-2/
IEEE
" » Python: Object-Oriented Programming Part 2." Richard Amin | Sciencx [Online]. Available: https://www.scien.cx/2022/10/18/python-object-oriented-programming-part-2/. [Accessed: ]
rf:citation
» Python: Object-Oriented Programming Part 2 | Richard Amin | Sciencx | https://www.scien.cx/2022/10/18/python-object-oriented-programming-part-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.