Inheritance and static members in C# vs Python

Imagine we are developing a game where a player fights a bunch of monsters. We would like to store several attributes for the player as well as the monsters. We quickly notice that the player and the monsters have so much in common. For example, they have a name, maximum health, weapon and in terms of functions, they can all walk, attack and get hurt. We realize that instead of defining all these attributes and functions for the player and all the monsters separately, we can create a blueprint called  Actor.

C# code

Inheritance

There is a lot more to classes than simply instantiating objects. In our Actor class above, we collected the attributes and functions that were common to the player and all the monsters. However, in addition to having common attributes, the player and monsters obviously have other attributes independent of each other. For example, the player might have multiple lives or a healing function while the monsters might have a search function. To implement these additional attributes and functions, we can create a class  Player and a class  Monster, both of which inherit from the  Actor class.

C# code

The  Player class inherits from the  Actor class using the colon operator :. This means that any object of the  Player class has all the attributes and functions in class  Actor as well as the additional attributes and functions in class  Player. Class  Actor, in this scenario, will be called the parent or base class and  Player will be called the child or derived class. Similarly the class  Monster is a child/derived class of  Actor.

When an object of the child class is instantiated, the constructor of the parent class is called first and then the constructor of the child class is called. Therefore, the child object must receive any parameters required by the parent constructor as well as the parameters required by the child constructor, as arguments. The child constructor then assigns the arguments to the parameters of the parent constructor using the colon  : operator and the base() function. The same example in Python is given below.

Python code

In Python, the child class inherits form the parent class using parenthesis. The child constructor assigns arguments to the parameters of parent constructor by calling the parent constructor as  super().__init__(argument_1, argument_2, ... ). Let us now test the functionality of our classes using the following code.


output_monsters2

Example: Class inheritance

Static members

The class attributes and member functions we have used so far were non-static. That is, there was a unique copy of the attribute or function for each instantiated object of the class. On the contrary, static members are common to the class. There is only one copy of a static member. Static member functions are called using the class name and the dot operator and can be called even when no objects have been instantiated. Suppose in our game example, we would like to keep track of the number of monsters spawned so far. We can use static members in our  Monster class.

C# code

Since the  Monster class constructor is automatically called every time a new Monster object is instantiated, we increment the static attribute  monster_count inside the constructor. Additionally, we define a static member function  getNumMonsters() that displays the value of  monster_count. The function getNumMonsters() can be called even before any objects have been instantiated as we do at line 9. We then created three new  Monster objects and call the function again to get the new value of  num_monsters.

static_members

Output: Use of static members

In Python, any class variable/attribute that is declared within the class definition but outside any member function is a static variable. A static member function is defined by the decorator  @staticmethod before the function.

Python code

 

< Introduction to classes              Difference between lists and dictionaries >

You may also like: