In this, we will start python OOPs from classes, first, we know what is python class and is it declare:
>>> class className:
<statement 1>
.
.
.
<statement 2>
The statements inside a class definition will usually be function definitions, but other statements are allowed, and sometimes useful.
Class Objects
In python, class objects are defined using two ways:
Attribute references
instantiation
Attribute references
Used for all attribute references in Python: obj.name
We can look it like:
>>> class Demo:
... i = 1
... def test(self):
... return 'hello'
In this, we can reference both variable and function, Demo.i or Demo.test and find these values as a result.
Instantiation
In this creating a new object of a class, which is like this -
x = Demo()
The instantiation operation (“calling” a class object) creates an empty object. Many classes like to create objects with instances customized to a specific initial state. Therefore a class may define a special method named __init__(), like this:
def __init__(self):
... self.value= []
Example:
>>> class Complex:
... def __init__(self, value1, value2):
... self.v1 = value1
... self.v2 = value2
...
>>>x = Complex(3.0, 4.5)
>>> x.v1, x.v2
Output - (3.0, 4.5)
Class and Instance Variables
Instance variables define inside the class constructor.
Look here:
>>> class pet:
... age = 15 # class variable
... def __init__(self, name):
... self.name= name # instance variable
...
>>>x = pet(jack)
>>> x.name
Output: jack
>>>x = pet.age
Output: 15
Define function outside the class
Yes, we can also define the function outside the class.
def f(self, name):
return self.name
>>> class pet:
... age = 15 # class variable
... f1 = f
... def fun(self):
... return "hi" # instance variable
... f1 = fun
Methods may call other methods
Yes we can also call one method with the help of another method
Methods may call other methods by using method attributes of the self argument:
Example:
>>>class Bag:
... def __init__(self):
... self.data = []
... def sub(self, x):
... self.data.append(x)
... def subtwice(self, x):
... self.sub(x)
... self.sub(x)
Inheritance
We can inherit the property of parent class using the base class as per below logic
>>> class DerivedClassName(BaseClassName):
<statement-1>
.
.
.
<statement-N>
Example:
>>>class Person: ... def __init__(self, fname, lname): ... self.fname1 = fname ... self.lname1 = lname ... def display(self): ... print(self.fname1, self.lname1)
>>>class Student(Person): ... def__init__(self, fname, lname): ... super().__init__(fname, lname) ... self.age = 25
x = Person("sachin", "raj")
x.display()
Multiple Inheritance
Python supports a form of multiple inheritances as well.
>>> class DerivedClassName(Base1, Base2, Base3):
<statement-1>
.
.
.
<statement-N>
Private Variables
Variables that cannot be accessed except inside an object don’t exist in Python.
It defines using add prefixed with an underscore, like _var
Thanks for reading Codersarts python OOps.
If you like Codersarts blog and looking for Assignment help, Project help, Programming tutors help and suggestion you can send mail at contact@codersarts.com.
Please write your suggestion in the comment section below if you find anything incorrect in this blog post
Comments