#this lecture is 53,54,55,56,57,58# Class is a template where we fill
the things according to requirements
class student:
pass # if we donot want to enter any method or function
# then we use pass other wise error will occure
.harry = student() #class object or instancetarry = student()
harry.name = "Harry" #set properties to object.
harry.standard = 12
harry.section = "A"
tarry.name = "Tarry"
tarry.section = "B"
print(f"My name is {harry.name}, the section of tarry is {tarry.section}")
tarry.subjects = ["urdu","math","Chemistry","Biology"]
print(tarry.subjects)
#55 instance or object and class veriables
class Employee:
no_of_leaves = 8
pass
ali = Employee()
sameer = Employee()
ali.name = "Ali"
ali.salary = 50000
ali.role = "Data Scientist"
sameer.name = "Sameer"
sameer.salary = 60000
sameer.role = "Android Developer"
print(Employee.no_of_leaves)
print(Employee.__dict__) # show attributes associated with employee class
Employee.no_of_leaves = 9
print(Employee.no_of_leaves)
#56 Use of Self in Class
class Employee2:
total_leaves = 8
def printDetails(self):
return f"name is {self.name}, Salary {self.salary}, role {self.role}"
Akram = Employee2();
Akram.name = "Akram"
Akram.salary = 54222
Akram.role = "MyRol"
print(Akram.printDetails())# here Akaram is self.
#57 Class Constructur __init__
class Employee3:
def __init__(self,ename,esalary,erole): #Creating Constructor
self.name = ename # assign the value to arugments
self.salary = esalary
self.role = erole
Shaiz = Employee3("Shaiz",2542,"Instuctor") # pass the value in Consturctor
print(f"Name: {Shaiz.name}, Salary: {Shaiz.salary}, Role: {Shaiz.role}") #print the values
#57 Class Mehtod
class Employee4:
leavs = 45
@classmethod
def change_leaves(cls, newleaves): # if we want to class level change then we can use the class
# class method to change it permanenetly.
cls.leavs = newleaves
return newleaves
asad = Employee4()
print(f"before changing the leaves: {asad.leavs}")
l = asad.change_leaves(34)
print("Asad New Leaves",l)
print("Asad old leaves: ",asad.leavs)
Amjad = Employee4()
print(Amjad.leavs)
# 58 Class method as alternative Constructor
class Employee5:
def __init__(self,ename,esalary,erole): #Creating Constructor
self.name = ename # assign the value to arugments
self.salary = esalary
self.role = erole
@classmethod
def from_dash(cls,string):
return cls(*string.split("-"))
Basit = Employee5.from_dash("Basit Ali-4511-student")
print(f"Name {Basit.name} Salary: {Basit.salary}, Role: {Basit.role}")