Object oriented programming in pythons
-
One of the ways of solving programming problems is by creating objects.
-
In python objects has two characteristics
- attributes - features of the objects
- behaviour - functions of the objects
-
Object consist of Class. Class is a blueprint of the object defined.
- Here we have created class animal.
- animalcount is a class variable. It can be used inside and outside the class as an attribute
- It consist of instance attributes, means which are created on the go and not predefined.
- A function sound is defined under the class,which is method used by the object or behaviour of the object.
- __init__ is a constructor to create the instances for the particular class
- dict − Dictionary which contains the class's namespace.
- doc − Documentation string inside the class. if undefined it returns none.
- name − Retuns Class name.
- module − Returns Module name in which the class is defined. The attribute is "main" in interactive mode.
- bases − Retuns the base classes, in the order of their occurrence in the base class list.
-
Inhertance is acquiring attributes and methods from another class.
-
class subclass(parent class) -->all the attributes of the parent class is inherited by the subclass.
-
if child class doesn't have a __init__ of its own it will inherit it from parent. if the constructor is present it will overide the constructor of parent class
-
super() --> the function is used to inherit the attributes and methods from parent class even if subclass consist of init method to overide it.
Here you can see if i am directly calling the parent attribute it is not accessible. It throws an attribute error. But when i call it from the child class which consist of parent class method. the value is accesible.