Tag: Perform a relationship between the LSE classes using features font col
One, the Complete property
1. Definition
After a method is disguised as a property, it should be possible to perform a property deletion and modification operation,
Additions and modifications correspond to the way the setter is decorated,
Deleting an attribute corresponds to the method that is deleter decorated.
@property: Disguise the method as a property
@ The method name that is decorated by the property. Setter:
When the method of the property decoration, and implemented a method of the same name, and was decorated by the setter decorator,
Then, when assigning a value to the decorated method, the method of decorating the setter decorator is triggered,
This method must pass a parameter to receive the value after the equals sign,
is used to protect a variable from being able to add some protection when modified.
@ The method name that is decorated by the property. Deleter:
When the method of the property decoration, and implemented a method of the same name, and was decorated by the deleter adorner,
The method that is decorated by the deleter decorator is triggered by the removal of the decorated method.
This method does not really delete this property at execution time, but it does what you do in the code.
2. Examples
Student ClassclassStudent:def __init__(self,name): Self.__name=name @propertydefname (self):returnSelf.__name@name. Setterdefname (self,new):ifType (new) isStr:#because the name is a string type, we can guarantee that the name can only be modified with a string.Self.__name=new @name. Deleterdefname (self):delSelf.__namexiaoming= Student ('Xiao Ming')Print(Xiaoming.name)#Xiao MingXiaoming.name= 123#not a string can not be modifiedPrint(Xiaoming.name)#Xiao MingXiaoming.name='Small tabby cat'Print(Xiaoming.name)#Small tabby catdelXiaoming.namePrint(Xiaoming.__dict__)#{} Empty dictionaryFruit Type:classFruits:__discount= 0.7def __init__(Self,price): Self.__price=Price @propertydefPrice (self):returnSelf.__price* Fruits.__discount@price. SetterdefPrice (self,new):ifType (new) isIntorfloat:self.__price=new @price. DeleterdefPrice (self):delSelf.__priceBanana= Fruits (10)Print(Banana.price)#Discounted price 7.0Banana.price= 9Print(Banana.price)#Discounted price 6.3delBanana.pricePrint(Banana.__dict__)#{} Empty dictionary
3. Summary:
The method name that is decorated by setter and deleter must be the same as the method name that is decorated by the property. Method names can be called without parentheses.
When assigning a value to a method that is decorated by a property, the method that is decorated by the setter is triggered, and when the method that is decorated by the property is deleted, the Del operation is triggered
Method of being deleter decorated.
Note: (Generally, the most used is the property, the other two to see the situation and use)
Second, class method:
Decorate with @classmethod
Called by the class name
The class method default parameter is represented by the CLS instead of the self
You can modify the properties of a class directly from the class, without instantiating
classFruits:__discount= 0.7#static properties of the class def __init__(Self,price): Self.__price= Price#private properties of the object@propertydefPrice (self):returnSelf.__price* Fruits.__discount@classmethoddefChange_discount (cls,new):#the class method default parameter is represented by the CLS instead of the selfCls.__discount=Newfruits.change_discount (0.6)Print(Fruits.__dict__)#' _fruits__discount ': 0.6
Characteristics of the class method:
Use only the resources in the class, and this resource can be referenced directly with the class name, then this method should be changed to a class method
Third, static method
Methods that are @staticmethod decorated, do not use namespaces in classes, and do not use object namespaces.
It is possible to pass a parameter, or not to pass a parameter, without a default parameter (SELF,CLS), which is equivalent to an ordinary method outside a class.
The difference is that the class name is required when calling. Method Name
class Student: @staticmethod def login (): print(' Login successful ') student.login ())
Summary of method properties in class Iv.
Class: Member: Standard consumer: Default parameter:
Static attribute Class/Object
class method Class CLS representation class
static method Classes
The method object self represents the object
The property method Object self represents the object
Note: Some members can be called with classes and objects, but they are recommended to be called by standard users.
Five
1, Isinstance: To determine whether an object is a known type
Print (type (123) is int) # True
Print (Isinstance (123,int)) # True
# isinstance can also detect the relationship between an object and a class (including inheritance)
# type cannot detect inheritance relationships
classA:PassclassB (A):Passa=A () b=B ()Print(Type (a) isA#TruePrint(Type (b) isB#TruePrint(Type (b) isA#FalsePrint(Isinstance (A,a))#TruePrint(Isinstance (B,B))#True#The subclass is the type of the parent class, but the parent class is not a child class typePrint(Isinstance (B,a))#TruePrint(Isinstance (A, B))#False
2. Issubclass: Detecting the relationship between class and class
Usage: Issubclass (child class, parent class)
class A: Pass class B (A): Pass Print (Issubclass (A, b)) # False Print (Issubclass (b,a)) # True
Python's property, class method, and static method