Python 3 - Advanced Python Programming by Madhu Sir | Leela Soft

Опубликовано: 05 Ноябрь 2024
на канале: Leela Soft
807
107

#LeelaSoft #Madhu #BPKTechTuts
Advanced Python:
Object Oriented Progeramming by Using Python:
Class based Programming:
What is a Class?
variables
methods
constructors
What is an Object?
Class creation in Python:
class Test:
pass

Object Creation in Python:
t1 = Test()

class Employee:
pass

e1 = Employee()

What is a constructor?
Name of the constructor in Python:
_init_
Types:
1.default constructor
2. parametarised constructor

2.parametarised constructor
class Employee:
def __init__(self):
print("Constructor - Zero Arg")

e1 = Employee()

Ex:
class Employee:
def __init__(self, eid, ename):
print("Emp Id and Name :{1}, {0} ".format(ename, eid))

e1 = Employee(101, "John")

Methods:
def method_name(params):
method body
return val/expresion

Types of Methods:
1.instance methods
2.class methods
3.static methods

1.instance methods
def m1(self):
pass

2.class methods
@classmethod
def m1(cls):
pass

3.static method
@staticmethod
def m1():
pass

def m1():
pass

Ex:
class Employee:
#Instance Method
def emp_info(self, eid, ename):
print("Emp Id and Name :{1}, {0} ".format(ename, eid))

e1 = Employee()
e1.emp_info(101, "John")

e2 = Employee()
e2.emp_info(102, "Scott")

e3 = Employee()
e3.emp_info(103, "Miller")

Ex:
class Employee:
#Class Method
@classmethod
def emp_info(cls, eid, ename):
print("Emp Id and Name :{1}, {0} ".format(ename, eid))

Employee.emp_info(101, "John")
e2 = Employee()
e2.emp_info(102, "Scott")
Employee.emp_info(103, "Miller")

Ex:
class Employee:
#Class Method
@classmethod
def emp_info(cls, eid, ename):
print("Emp Id and Name :{1}, {0} ".format(ename, eid))

@classmethod
def emp_disp(cls):
return cls()

e1 = Employee.emp_disp()
print(type(e1))

e1.emp_info(101, "John")
e1.emp_info(102, "Miller")

Ex:
class Employee:
@staticmethod
def emp_disp():
print("static Method")

e1 = Employee()
e1.emp_disp()

Employee.emp_disp()

Ex:
class Employee:
def emp_disp():
print("static Method")

Employee.emp_disp()

Variables:
1.instance
2.static
3.local

1.Instance Variable:
Creation:
1.Within the class we can create in constructor and instance method by using self parameter.
2.Out side of the class we can create by using object.

class Employee:
def __init__(self):
self.eid = 10
self.ename = "John"

def m1(self):
self.x = 10
self.y = 20

e1 = Employee()
e1.z = 30

print(e1.__dict__)
e1.m1()
print(e1.__dict__)

Accessing:
1.Within the class we can access in constructor and instance method by using self parameter.
2.Out side of the class we can access by using object.

Ex:
class Employee:
def __init__(self):
self.eid = 10
self.ename = "John"
print("Construcot: ", self.eid, self.ename)

def m1(self):
self.x = 10
self.y = 20
print("Instance Method: ", self.eid, self.ename)
print("Instance Method: ", self.x, self.y)

e1 = Employee()
e1.z = 30
e1.m1()
print("Out side of the class :", e1.z)
print("Out side of the class :", e1.eid, e1.ename)
print("Out side of the class :", e1.x, e1.y)

2.Static Variables:
"SRKR"

Creation of a static variables:
With in the class we can create directly
With in the constructor we can create by using class_name
With in the instance method we can create by using class_name
With in the class method we can create by using class_name
With in the class method we can create by using 'cls' parameter
With in the static method we can create by using class_name
Out side of the class we can create by using Class_Name

class Test:
cname = "SRKR"
def __init__(self):
Test.x= 10
print("Construcot: ", Test.x, Test.cname)

def m1(self):
Test.y = 10
print("Instance Method: ", Test.cname, Test.x, Test.y)

@classmethod
def m2(cls):
Test.z = 10
cls.a = 100
print("Class Method by using cname: ", Test.cname, Test.x, Test.y, Test.z, Test.a)
print("Class Method by using cls param: ", cls.cname, cls.x, cls.y, cls.z, cls.a)

t1 = Test()
Test.b = 200
t1.m1()
t1.m2()
print("Out side of the class :", Test.z)

3.Local Variable:
Creation:
With in the constructor, method and parameters

class Test:
def __init__(self, a):
x= 10
print("Construcot: ", x, a)

def m1(self):
y = 10
print("Instance Method: ", y)

@classmethod
def m2(cls):
z = 10
print("Class Method: ", z)

t1 = Test(100)
t1.m1()
t1.m2()

OOP Principle:
1.Encapsulation
2.Inheritance:
Single Level
Multi Level
Hirarichle
Multiple
Hybrid
Mutipath
Cyclic
3.Polymorphysm:
Overloading:
compilation time
operator loading
Overriding:
run time
Method
Constructor
4.Abstraction
abc, ABC, abstractmethod

Packages:
__init__.py

Exception Handling:
try, except, finally, raise

File Handling:
File, Database

f = open("sample.txt", "w")

Database:
MySQL, SQLite,
pip

Regular Expressions:
Form Validation
re
Web Scrapping:
urllib, request, urlopen()

Multithreading:
threading, thread

Graphics:
Logo
turtle

GUI:
tkinter provided by Python
PyQt
kiwi