Property built-in adorner functions are used only in object-oriented
fromMathImportPiclassCircle:def __init__(self, R): SELF.R=R @property#Some methods that have similar properties that require manipulation are disguised as object properties defperimeter (self):return2 * pi *SELF.R @propertydefArea (self):returnPI * SELF.R * * 2Circle= Circle (5)Print(Circle.perimeter)#calling methods and manipulating properties are essentially the same methodPrint(Circle.area)
Property provides viewing, modification, and deletion of private properties
classPerson :def __init__(self, name): Self.__name=name @propertydefname (self):returnSelf.__name@name. Setter#modify by assigning values to modify defname (self, new_name): Self.__name=new_name @name. Deleterdefname (self):delSelf.__nameP= Person ('Zxc')Print(P.name)#ZxcP.name ='ZXF'Print(P.name)#ZXFdelP.name#here del just corresponds to the @name.deleter, mainly see @name.deleter the following method to perform what#print (p.name) # Error shows no Name attribute
Classmethod and Staticmethod
classPerson :__key='Human' def __init__(self, name): Self.__name=name @propertydefname (self):returnSelf.__name@classmethod#turn a method into a method in a class so that the method can be called directly by the class without relying on the object defChange_key (CLS, New_key): CLS.__key=New_keyPrint(Person._person__key)#HumanPerson.change_key ('Human')Print(Person._person__key)#Human#Classmethod should be used to decorate this method when the operation of this method involves only static properties.Classmethod
classLogin:def __init__(Self,name,password): Self.name=name Self.pwd=PassworddefLogin (self):Pass@staticmethod#decorate a static method that does not have a relationship with a class or object, called by the class defGet_usr_pwd ():#Static Methodsusr = input ('User name:') PWD= Input ('Password:') Login (usr, pwd)#generally used in fully object-oriented programmingStaticmethod
Python Object-oriented advanced