Property: A method is disguised as a property that does not have an intrinsic elevation at the level of the code, but makes it look reasonable
Masquerading as a property
Class Person:
def _ _init_ _ (Self,name, weight,hight):
Self.name=name
Self.weight=weight
Self.hight=hight
def BIM (self):
The BMI for return%s is%s% (Self.name, (Self.wejght/(self.hight**2)))
P1=person ("haha", 50,1.63) # instantiates an object
P1.bmi () # This way the direct invocation of the method in the class can get the object's BMI
But the resulting BMI is calculated as a function method,
Normally BMI is a noun attribute so we can
It is disguised as a property through the @property adorner.
Disguise it as a property through the @property adorner:
Class Person:
def _ _init_ _ (Self,name, weight,hight):
Self.name=name
Self.weight=weight
Self.hight=hight
@property # adorners disguise the following methods as properties of an object
def BIM (self):
The BMI for return%s is%s% (Self.name, (Self.wejght/(self.hight**2)))
P1=person ("haha", 50,1.63)
Print (P1.BMI) ====> Gets the result # object name. property to execute directly
But if there is an age parameter, how do we change the parameters? See the example below
e.g
Class Person:
def _ _init_ _ (Self, name, age):
Self. Name= Name
# # Self. _ _age = Age # is confidential as a private member
Self. _ _age = Age If Type (age) is int else print ("Input format not input number")
# This is the IF statement written in ternary arithmetic
Let the number you enter must be a numeric type, unless the display is not
@property
def Age (self): # Set a Masked property method
return self. _ _age returns self. _ The value of _age
@ Age The . Setter # Adds a property that can be modified by the age camouflage property and changes in the function and adorner must be in phase
def age (Self, A1 ): the back A1 receives the same age parameter that you want to change.
Self. _ _age = A1 if type (A1) is int else print ("Input format not input number")
# This is the IF statement written in ternary arithmetic
Let the number you enter must be a numeric type, unless the display is not
Print (666)
@ age. deleter # Delete
def age (self):
del self._ _age # Delete self.
P1=person ("haha", 20) # instantiating an object automatically executes _ _init_ _ incoming parameter Judge age and then go down.
P1.age=18 # This is a modification to the property
When the function executes to this step, the @age is executed first. Setter
Then legend the value behind to A1 and then down to the input value
Print (p1.age) = = = > 18 666 The final result is a modified value
Del p1.age # Execute Delete self._ _age this will trigger @age. deleter
Print (P1._ _dict_ _) = = > {name: haha}
Class method
: A class method called by the class name,
The first parameter in a class method is a CLS-
Python automatically passes the class name, which is class space, to the CLS
: Invoking a class method from an object
The class itself is passed to the CLS
Class A:
def func (self):
Print (self)
@ Classmethod # class method
def func1 (CLS):
Print (CLS)
A1=a ()
A1.func () # is equivalent to the object name. Methods in the method invocation class ===> <__main__. A Object at 0x00b10f50>
A. Func (A1) gets function memory address is not commonly used to get memory address in work
A1.FUNC1 () # Object calls class method in class the class itself is passed in
Equivalent to A (). Func1 () ===> <class ' __main__. A ' >
is the class space of a Class A.
A.FUNC1 () # In a class that has a class method
The class name. Class method () is equivalent to passing class space to the first argument of a class method
So get Class A class space ===> <class ' __main__. A ' >
Application Scenarios for class methods
1. There are some methods in the class that do not need to pass in the object, not anything
e.g
Class A:
Name = "Alex"
Count=1
def func (self):
return a.name + str (a.count+1)
A=a ()
Print (A.func ())
Class A:
Name = "Alex"
Count =1
@classmethod
def func (CLS):
return cls.name + str (cls.count+1)
A=a ()
Print (A.func ())
2. Use the class method when changing static variables in a class
Class A:
Age=12
@classmethod
def func (CLS):
Cls.age=20
A=a () instantiates an object
A.func () passes class space to the CLS and assigns an age of 20 to it
Print (a.age) ===> get results 20
3. Inheritance in which the parent class gets the space of the child class
The class method in the parent class gets the space of the child class and can do anything about it
Take any action on a child class
e.g
Class A:
def func (self):
Print (self)
@classmethod
def func1 (CLS):
Print (CLS)
Class B (A):
def F1 (self):
Pass
B.FUNC1 () ===><class ' __main__. B ' > now looking for func1 in class B,
To Class A, class B will be passed to the CLS.
Because class B calls the class method, it gets the class space of B.
Class A:
Age=12
@classmethod #类方法
def func (CLS): # Passing class space to CLS
CLS.AGE=50 # To change the class space after you get the class space changes the age in B to 50
str = "JJJ" # to change the class space after the class space is changed the STR in B is changed to JJJ
Print (Cls.age)
Class B (A):
Age=22
Str= "DGG"
B.func () # Get class space by class name + class method name
Print (b.age) = = = > 50
Print (B.STR) =====> JJJ
Common method
Class A
Age = 12
def func2 (self): Objects #self subclasses can also get the contents of subclasses but cannot change
Print (self)
Class B (A):
age=11
B=b ()
Print (B.age)
Static methods: No objects are required, no arguments are required to be used directly when calling
Pros: 1. Static method is clearer in code block viewing angle
Compared to ordinary static methods, it is just a method, if you write a bunch of methods put together, too messy not to find
Put it in a class that you can quickly find and then call directly through the class name
2. Increased reusability of the code
Class A:
@staticmethod
def func ():
Print (666)
A.func () ===> 666
python=== Property-Class method =====20