Return to site

Python Slots Inheritance

broken image
Python slots inheritance online
  1. From Python documentation: slots allows us to explicitly declare data members (like properties) and deny the creation of dict and weakref (unless explicitly declared in slots or available in a parent.) So, how does it relate to the issues I've mentioned?
  2. This Python3: Deep Dive Part 4 course takes a closer look at object oriented programming (OOP) in Python. MAIN COURSE TOPICS. What are classes and instances. Class data and function attributes. Instance, class and static methods. Polymorphism and the role special functions play in this. Single inheritance.

The slots machine, Python Slots And Inheritance often known as the Python Slots And Inheritance 'one armed bandit', became an icon of modern online gaming. At Slotomania, you can start playing your favorite slot games with crazy graphics, top of the line sound. Furthermore, attrs has been around for a while and is supported in Python 2.7 as well as Python 3.4 and up. Continuing our consideration of slots in Python 3, we take a look at issues of inheritance and there's also a really useful pickling tip, with or without.

Previous Chapter: Multiple Inheritance
Next Chapter: Magic Methods and Operator Overloading
Python Slots Inheritance
  1. From Python documentation: slots allows us to explicitly declare data members (like properties) and deny the creation of dict and weakref (unless explicitly declared in slots or available in a parent.) So, how does it relate to the issues I've mentioned?
  2. This Python3: Deep Dive Part 4 course takes a closer look at object oriented programming (OOP) in Python. MAIN COURSE TOPICS. What are classes and instances. Class data and function attributes. Instance, class and static methods. Polymorphism and the role special functions play in this. Single inheritance.

The slots machine, Python Slots And Inheritance often known as the Python Slots And Inheritance 'one armed bandit', became an icon of modern online gaming. At Slotomania, you can start playing your favorite slot games with crazy graphics, top of the line sound. Furthermore, attrs has been around for a while and is supported in Python 2.7 as well as Python 3.4 and up. Continuing our consideration of slots in Python 3, we take a look at issues of inheritance and there's also a really useful pickling tip, with or without.

Previous Chapter: Multiple Inheritance
Next Chapter: Magic Methods and Operator Overloading

Python Slots Inheritance Online


Multiple Inheritance: Example


Robot Classes

This chapter of our tutorial is meant to deepen the understanding of multiple inheritance that the reader has built up in our previous chapter. We will provide a further extentive example for this important object oriented principle of the programming language Python. We will use a variation of our Robot class as the superclass. We will also summarize some other important aspects of object orientation with Python like properties. We will also work out the differences between overwriting, overloading and overriding.

This example has grown during my onsite Python training classes, because I urgently needed simple and easy to understand examples of subclassing and above all one for multiple inheritance.

Starting from the superclass Robot we will derive two classes: A FightingRobot class and a NursingRobot class.

Finally we will define a 'combination' of both the FightingRobot class and the NursingRobot class, i.e. we will implement a class FightingNurseRobot, which will inherit both from FightingRobot and NursingRobot.

Let us start with our Robot class: We use a private class attribute __illegal_names containing a set of names not allowed to be used for naming robots.

By providing an __add__ method we make sure that our robots are capable of propagating. The name of the resulting robot will be automatically created. The name of a 'baby' robot will consist of the concatenation of the names of both parents separated by an hyphen. If a parent name has a name containing a hyphen, we will use only the first part before the hyphen.

The robots will 'come to live' with a random value between 0 and 1 for the attribute health_level. If the health_level of a Robot is below a threshold, which is defined by the class attribute Robot.__crucial_health_level, it will need the nursing powers of a robot from the NursingClass. To determine if a Robots needs healing, we provide a method needs_a_nurse which returns True if the value is below Robot.__crucial_health_level and False otherwise.

We can test the newly designed Robot class now. Watch out how the hyphened names change from generation to generation:


Python Inheritance Examples

Subclass NursingRobot

We are ready now for subclassing the Robot class. We will start by creating the NursingRobot class. We extend the __init__ method with a new attribute healing_power. At first we have to understand the concept of 'healing power'. Generally, it makes only sense to heal a Robot, if its health level is below 1. The 'healing' in the heal method is done by setting the health_level to a random value between the old health_level and healing_power of a ǸursingRobot. This value is calculated by the uniform function of the random module.

Let's heal the robot class instances which we created so far. If you look at the code, you may wonder about the function chain, which is a generator from the itertools module. Logically, the same thing happens, as if we had used first_generation + babies, but chain is not creating a new list. chain is iterating over both lists, one after the other and this is efficient!

An interesting question is, what would happen, if Hubert and Emma get added. The question is, what the resulting type will be:

We see that the result of addition of two nursing robots is a plain robot of type Robot. This is not wrong but bad design. We want the resulting robots to be an instance of the NursingRobot class of course. One way to fix this would be to overload the __add__ method inside of the NursingRobot class:

This is also bad design, because it is mainly a copy the original function with the only exception of creating an instance of NursingRobot instead of a Robot instance. An elegant solution would be having __add__ more generally defined. Instead of creating always a Robot instance, we could have made it dependent on the type of self by using type(self). For simplicity's sake we repeat the complete example:

Subclass FightingRobot

Unfortunately, our virtual robot world is not better than their human counterpart. In other words, there will be some fighting going on as well. We subclass Robot once again to create a class with the name FightingRobot.

What about Rambo fighting Terminator? This spectacular fight can be viewed in the following code:

An Example of Multiple Inheritance

Python Slots Inheritance Money

The underlying idea of the following class FightingNurseRobot consists in having robots who can both heal and fight.

We will instantiate two instances of FightingNurseRobot. You can see that after creation they are capable to heal themselves if necessary. They can also attack other robots.

Previous Chapter: Multiple Inheritance
Next Chapter: Magic Methods and Operator Overloading





broken image