• Class − A user-defined prototype for an object that defines a set of attributes that characterize any object of the class. The attributes are data members (class variables and instance variables) and methods, accessed via dot notation.
• Class variable − A variable that is shared by all instances of a class. Class variables are defined within a class but outside any of the class's methods. Class variables are not used as frequently as instance variables are.
• Data member − A class variable or instance variable that holds data associated with a class and its objects.
• Function overloading − The assignment of more than one behavior to a function. The operation performed varies by the types of objects or arguments involved.
• Instance variable − A variable that is defined inside a method and belongs only to the current instance of a class.
• Inheritance − The transfer of the characteristics of a class to other classes that are derived from it.
• Instance − An individual object of a certain class. An object obj that belongs to a class Circle, for example, is an instance of the class Circle.
• Instantiation − The creation of an instance of a class.
• Method − A special kind of function that is defined in a class definition.
• Object − A unique instance of a data structure that's defined by its class. An object comprises both data members (class variables and instance variables) and methods.
• Operator overloading − The assignment of more than one function to an operator.
Creating Classes
The class statement creates a new class definition. The name of the class immediately follows the keyword class followed by a colon as follows:
Here is a practical example:
Output:
We have a class Person that possesses Class members name and age and has 2 methods: __init__ and details. __init__ is the class constructor, it is used to build the object itself. The example above is a parameterized constructor. Parametrized constructors allow us to initialize an instance member when we create an object class. For example, in this type of constructor, the variables within the class are fed to the function __init__.
Self is used to specify that the attribute/variable belongs to this particular object. When you want to alter an inner variable of a class, it will automatically refer to that particular variable.
Output:
This is a default constructor. All Person objects created will always be initialized with name = “Alex” and age = “41”.
Built-In Class Attributes
Every Python class keeps following built-in attributes and they can be accessed using the dot operator like any other attribute:
• __dict__ −> Dictionary containing the class's namespace.
• __doc__ −> Class documentation string or none, if undefined.
• __name__ −> Class name.
• __module__ −> Module name in which the class is defined. This attribute is "__main__" in interactive mode.
• __bases__ −> A possibly empty tuple containing the base classes, in the order of their occurrence in the base class list.
But there also exist user-defined attributes pretty much like variables inside the class.Example:
Class Person has an inner variable which will always be “Bob” because its value is never changed through the constructor and can be accessed like calling a method but without the parenthesis: person1.real_name.
Inheritance
One of the most used features when programming with objects is inheritance. It is often used so every component/object in a system has some sort of relation. So instead of just creating a new class, we can create a subclass which will inherit all attributes from the parent class:
Here is a practical example for our Person class:
Output:
We created the classes Worker and Elder that are a subclass of Person and therefore inherit the method details() from Person, but we also implemented a new method is_working() which only exists in these subclasses and not in Person. Inheritance only happens in one way which is down. You can use issubclass() or isinstance() functions to check a relationship of two classes and instances.
• The issubclass(sub, sup) boolean function returns true if the given subclass sub is indeed a subclass of the superclass sup.
• The isinstance(obj, Class) boolean function returns true if obj is an instance of class Class or is an instance of a subclass of Class
But we can have multiple inheritances and add another subclass:
Output:
Overloading
Overloading is the ability of a function or an operator to behave in different ways. It can be widely used in python classes, where it is possible to redefine how base/built-in class methods work like __init__. These are a shortlist of overloading methods.
Method, Description & Sample Call |
__init__ ( self [,args...] ) Constructor (with any optional arguments) Sample Call : obj = className(args) |
__del__( self ) Destructor, deletes an object Sample Call : del obj |
__repr__( self ) Evaluable string representation Sample Call : repr(obj) |
__str__( self ) Printable string representation Sample Call : str(obj) |
__cmp__ ( self, x ) Object comparison Sample Call : cmp(obj, x) |
The most common example is the Vector class to which we redefine how __str__ and __add__ work in order to be more personalized to that class. Let’s see:
We personalized how the print(vector) will work with __str__ and added a base method __add__ which lets us just perform an addition between two vectors like they were integers.
Overriding
With overriding, it is possible to change how a method is implemented in a class, in other words, we can change the methods in the subclass that were inherited by the parent class so it works in a more specific way,
The details() method belonged to the class Person, but in the subclass Elder, we say “But I want it to work more specifically within this particular subclass” overriding what the original method would do. If that method wouldn’t have been overridden, it would have worked by default printing the Name and Age accordingly.
But attributes can also be overridden in subclasses,
Output:
In the example above, the subclass Alien, we override the attribute race from “Human” to “Alien” and also we override the details function from the parent Class Person().
Data hiding
In python, with data hiding, it is possible to prevent the functions of a program to be accessed directly on the program. It is similar to private and protects class definitions in other programming languages. For data hiding, you need to name attributes with a double underscore prefix, and those attributes then are not directly visible to outsiders.
Output:
This will change the __hiddenVariable content because it calls the add(increment) method and because that method is within the domain of the class it will “see it” but if we try to print it like in the last line it will produce an error saying that it doesn’t exist. This is a very common way to protect attributes since they can’t be accessed externally.