Today, Python has most demand in IT industry. And everybody want to learn and make career in Python.
In previous article we have seen some basic of Python, so you can start learning it by yourself. Today, we will learn about Classes and its members, like constructor, class variable, local variable and class functions. We will also check some built in attributes available with classes.
My previous article in Python: https://ntsh-vicky.medium.com/basic-of-python-edd5ec6845e5
Object Oriented Programming Concept (OOPs)
Before learning classes and objects, you should know about OOP’s Concept. There is a lots of articles you can find in OOP’s concept, just you need to search in Google.
An Object Oriented programming aims to implement logic of real world entities in programming, like classes, objects, inheritance, hiding, polymorphism etc.
- Classes : A class is a blueprint or prototype. This is not a physical things. In real life example, Human is a class, vehicle is a class, car is a class. Here, human, vehicle and car does not represent a specific thing but a whole community. If you think about human, then here boy, girls, women, man, your father, your mother, you, me; every body is human. No specific one. Vehicle, whatever you are looking at road, car, truck, bike etc.
It represents the set of properties or methods that are common to all objects.
In Python we declare a class using class keyword, following with the name of class.
Syntax : class <class name here>
Example: class Vehicle
2. Object : An object is a basic unit of object oriented programming and specified a specific class. As class human, you are an object, i am an object. A bike you have is an object. Whatever you are looking is an object. So an object always has a state, behavior and an identity.
Like me, state: ethnicity, color, body; behavior: angry, happy, eating etc; identity: my name e.i. Nitish Srivastava.
Like your bike, state: running, stand, might be flying also if you are a stuntman; behavior: horn, break etc. Identity: its vehicle number…
Above image contains a class named, Vehicle. Here you can see, Vehicle class is declared by keyword class and contains some member inside it. In line number 4, msg is a class variable, which initialized with a default string message “This is vehicle class”.
Below in line number 6 and 9, class Vehicle contains two member function showMsg and listenHorn respectively. showMsg has a statement, which printing whatever msg conatins (line number7) and listenHorn, first initializing a local variable horn (in line number 10) and then printing it (in line number 11). And you can see, both function has one parameter self.
By default in Python, every member function has first parameter self. So while creating these functions, you need to write at least one argument in functions. Here i have created this argument as named self. You can name anything, but self is meaningful.
The first parameter of a function of a class, referenced object of its class. Its same as this keyword in Java. You can named it anything. If you have to call any class member, you need to call with this parameter’s given name. As below image, i have changed name of the function parameter self to hhh.
You can see, in line number 7, i am calling msg variable with parameter (hhh.msg), but not horn, in line number 11. Always remember, a class member you need to call with first parameter of function, or outside class with the name of the object of the class (like line number 16). But, because horn is not a class member but local member of function listenHorn, you no need to write hhh (or self) with it. Also you can’t call variable horn (or any local variable) with object as given in line number 18.
Now come to see what happening from line number 13.
v = Vehicle(), this is the way to initialized object in Python (in Java or C++, same you are doing with new keyword). here name of the object is v, which is an object of class Vehicle. So if you want to use member of the class, like its class variable or functions, you need to call it through object.
You can see, in line number 16, i have reinitialized variable msg. and in output of line number 17, it has changed. But i have also tried to reinitialized variable horn in line number 18, but it has not changed in the output of line number 19. It is still Peep Peep.
Lets an another example:
In the above image, i have created a new function showName with two parameter. Same first parameter referenced object of the class and second parameter what you have to pass. So in line number 14 and 19, you can see i am passing a string value in function showName. In line 14, Bullet 400 with object name v, and in line 19, Classic 350 with new object v1. Wherever, there is two parameter showName contains, so why i am passing only one string parameter, and line number 10 statement is showing second parameter’s variable. Because as we already talked, first parameter is referenced to object of the class, so you have to work with second or next parameter if you have.
Also you can see, two different object is not affecting each other. As line 23, has not changed because of line 20.
In above image, check line number 22,23,25,26, how actually self works.
3. Constructor: In the above image, line number 7, is an example of constructor. In another programming language, like C++ or Java, you have learnt about constructor is the class member function, has same name as class name, without any return type defined and called when an object of this class is created.
In Python, we declare constructor using __init__(self) function. It is a predefined name. Use is same, it called when you are creating object of its class. Like in line number 17 and 19 in above image, constructor directly called when initializing an object. In above example, i have also passed a string parameter with class, which is passing through name variable in constructor. You can pass any number of arguments inside function, but first one is default referenced to object of the class.
How to use function and its argument you can learn in my previous article https://ntsh-vicky.medium.com/basic-of-python-edd5ec6845e5#66be . You can use *args or *kargs for number of argument.
4. Inheritance: Inheritance is an another features of Object Oriented Programming. Its provide re-usability of the code.
In real life example, Human, it can walk, run, talk, eat etc. If there is sub class of human like Boy and Girl, both have same attribute or behavior, but there is some sub class has its own attribute. Like Boys can stunt (these days normal for girls too). Girls can birth, but not boys. So walk, run, talk, eat is basic attribute of a super class human, but some specific attribute a sub class may have like birth for class Girl. Same if super class is Vehicle and sub class is FourWheeler and TwoWheeler, then both contains all the features of super class but with their own specific feature.
Inheritance is the process of building a new class based on the features of another existing class. Check the below example on vehicle and its output.
You can use inheritance while calling database configuration file on another file, If database configuration will be same for all.
5. Polymorphism: The word polymorphism means having many forms. In some programming language, polymorphism is also described as same function name but different signature of arguments, that is called overloading. Also some times base class has same function name as super class with same signature, is called overriding.
You can’t use same function name and different parameter logic in Python. As below given image, you will see error like that….
But, you can use polymorphism with inheritance while overriding function in base class, as in image given below.
You can see in the above image example, there is two sub class extending one super class Vehicle. Both sub class has one function showMsg, same name as defined in super class Vehicle. While printing, both are printing constructor (self.__class__.__name__ returns name of the class of the object) of class Vehicle, but with their own function showMsg. Because here they are overriding showMsg of class vehicle with their own function.
If you want to print showMsg of class Vehicle, you have to create its own object, v3 = Vehicle(), and then call showMsg like v3.showMsg().
6. Access Modifier: It is used to restrict access to the variables and methods of the class.
There is three types of access modifier:
- Public Access Modifier
- Protected Access Modifier
- Private Access Modifier
- Public Access Modifier : The members of a class that are declared public are easily accessible from any part of the program. All data members and member functions of a class are public by default.
- Protected Access Modifier : The members of a class that are declared protected are only accessible to a class derived from it. Data members of a class are declared protected by adding a single underscore ‘_’ symbol before the data member of that class.
- Private Access Modifier : The members of a class that are declared private are accessible within the class only, private access modifier is the most secure access modifier. Data members of a class are declared private by adding a double underscore ‘__’ symbol before the data member of that class.
Example of Public Access Modifier : msg = “Hello World”
Example of Protected Access Modifier : _msg = “Hello World”
Example of Private Access Modifier : __msg = “Hello World”
In python protected member can also used by the object of the class in another class, as example below in line number 19….
Some basic command on Python you can try to learn about any classes or object or keyword in Python
- Know the name of class :- <objectname>.__class__.__name__ , like above examples self.__class__.__name__ or v.__class__.__name__
- Know built in attributes to use with objects:- dir(objectname) or dir(keywords). like dir(list) will return name of all built in functions or attributes of list.
- See the details and help documentation of any keywords :- help(keywordname), like help(list)
Thanks. Always try to write your code with OOPs functionality, because of Encapsulation and Re-usability.
(Sorry for grammatical mistakes)