What are class methods and static methods in python object-oriented programming
This article to share with you is about python object-oriented programming class methods and static methods is what, Xiaobian think quite practical, so share to everyone to learn, I hope you can read this article after harvest, not much to say, follow Xiaobian to see it.
Learn object-oriented programming in python today-class methods and static methods.
Create a python file named py3_oop3.py and write the operation code in this file:
#Object Oriented Programming #Class Methods and Static Methods
class Employee: raise_amount = 1.04#Define class variables num_of_emps = 0 def __init__(self,first,last,pay): self.first = first self.last = last self.email = first + '. ' + last +'@email.com' self.pay = pay Employee.num_of_emps +=1
def fullname(self): return '{} {}'.format(self.first,self.last)
def apply_raise(self): self.pay = int(self.pay * self.raise_amount) #Class methods are decorated with @classmethod identifiers #cls is used as the first parameter to represent the class itself. #Used in class methods, class methods are methods that are only related to the class itself #and not related to the instance @classmethod def set_raise_amt(cls,amount): cls.raise_amount = amount #Defines a class method that receives emp String #returns an instantiated object @classmethod def from_emp_str(cls,emp_str): first,last,pay = emp_str.split('-') #This is understood as calling #Employee(first,last,pay) #and return return cls(first,last,pay) #static method decorated with @staticmethod identifier #just like a normal function #determine if it is a workday @staticmethod def is_workday(day): if day == 5 or day ==6: return False return True
emp_1 = Employee ('T','Bag',50000)emp_2 = Employee ('Mc','User', 6000)Employee.set_raise_amt (1.05)print (Employee.raise_amount)#1.05print (emp_1.raise_amount)#1.05print (emp_2.raise_amount)#1.05#We call emp_1.set_raise_amt()#in Print Employee.set_raise_amt (1.06)print(Employee.raise_amount)#1.06print(emp_1.raise_amount)#1.06print(emp_2.raise_amount)#1.06#Find all raise_amounts of class and instance objects change #We print emp_1 attribute information print(emp_1.__ dict__)#{'first': 'T','last': ' Bag','email': 'T. email.com'pay': 50000}#This does not contain the raise_amount attribute #because calling the class method set_raise_amount #modifies the variable attributes of the class
#Define an emp string#Call from_emp_str()emp_str = 'T-Bag-5000'new_emp_1 = Employee.from_emp_str(emp_str)print(new_emp_1.email)#T. Bag@email.com#Call class Employee static method:import datetimetoday = datetime.datetime.today ()print(Employee.is_workday(today))#True
Run Results:
1.051.051.061.061.06 {'first': 'T','last': ' Bag','email':'T. email.com'pay': 50000}T.Bag@email.comTrue The above is what class methods and static methods are in python object-oriented programming. Xiaobian believes that some knowledge points may be seen or used in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.