Python – Class and Object
As we read earlier about Python Frozenset , We have finished all collection data type. Now we will study about a new concept i.e. “Python Object Oriented Programming”. In this we will cover what is object oriented programming , python class , python class example , python objects , etc. so lets start study about basic python oop.
Introduction to Object Oriented Programming :
Like other general purpose languages, python is also an Object Oriented Programming ( OOP ) since its beginning. It allows us to develop new applications using an object-oriented approach. It is good for defining abstract datatypes. It is easy to maintain and modify python code as new objects can be created with small difference to existing ones.
What is a class in python ?
class is a keyword that is used to create user defined data type is called a class.
Class syntax python :
class ClassName: statement-- ----------- -----------
Guidelines while creating any class :
- Thinking about the name of the class.
- Thinking about the attributes or variables of the class.
- Thinking about the operations or functions of the class.
Python class example : How to create a class in python ?
class Class1: c = 10 print(Class1)
The _ _init_ _( ) function in python:
The _ _init_ _( ) function is an important & special method in python class. It is used to initialize the object’s state or to initialize the attributes of a class. It is also known as a constructor.
_ _init_ _( ) syntax python :
def __int__(self): statement - - - - - - - -
_ _init_ _( ) example python :
class Customer: def __int__(self): self.id = 0 self.name = " "
The self parameter in python
The word “self” is a keyword that is used to represent the instance of the class. we can access the attribute and methods of the class with the help of using “self” keyword. It is not compulsory to be named self, you can call it whatever you like but it has to be the first parameter of any function in the class.
class Customer: def __int__(hello): hello.id = 0 hello.name = " "
The _ _str_ _( ) function in python :
The _ _str_ _( ) function is a built-in-function. It is used to compute an informal string representation of that object.
_ _str_ _( ) syntax python :
def __str__(self): statement- - - - - - - - -
_ _str_ _( ) example python :
class Customer: def __int__(self): self.id = 0 self.name = " " def __str__(self): s1 = "id :" + str(self.id) + "name :" + str(self.name) return s1
What is an object in python ?
An object is an instance of a class. we can use the ClassName to create the objects.
Object syntax python :
variable Name = ClassName( )
Object example python : How to create an object in python ?
# create a class named Class1: class Class1: c = 20 # create an object named obj : obj = Class1()
How to print the address of the given object ?
class Class1: c = 20 obj = Class1() print(obj)
<main.Class1 object at 0x000001DF3BF49430>
In the above example, how to print the value of c ?
class Class1: c = 20 obj = Class1() print(obj.c)
The above explained things can be understood well with the help of the following example.
class Person: def __int__(self): self.id = 0 self.name = " " def __str__(self): s1 = "id :" + str(self.id) +"\n" "name :"+ str(self.name) return s1 obj = Person() obj.id = "001" obj.name = "John" print(obj)
Static Method in python
A function followed by @staticmethod decorators is known as static method. static method works against object-oriented programming. Generally we are using static variable to declare variables as global.
It has following limitations :
- We can access static method using ClassName followed by Dot operator.
- We can use only static variables or local variables of functions within static functions.
- We can’t use “self” keyword in static functions.
General syntax of static method :
@staticmethod def func(arg1, arg2, _ _ _ ): statement - - - - - - - - - - - - - - -
Class Method in python
A function followed by @classmethod decorators is known as class method. It is an expression that gets evaluated after your function is defined.
General syntax of class method :
@classmethod def func(cls, argument) : statement - - - - - - - - - - - - - - -
This is a picture in which you can identify static, instance and local variable of function easily.
Access Specifier in python
By default, python do not support any access specifier like Private Public and Protected.
But we can use this type of behavior using _ (underscore) for protected member and _ _ (double underscore) for private member.
- Private : Variables are accessible only within class.
- Protected : Variables are accessible only within class and within derived class.
- Public : Variables are accessible anywhere.
class Class1: def __int__(self): self.id = 0 # Public self._name = "" # Protected self.__mobile = "" # Private obj = Class1( ) obj.id = 20 # Public obj._name = "abc1" # Protected obj.__mobile = "987654321" # Private
The Pass Statement in python
class cannot be empty, but if you for some reason have a class with no content, put in the pass statement to avoid an error.
class Class1: pass
«Previous Next» >