1.staticmethod: (static method) static method: Allows a method in a class to be called directly by a class, just like a normal function.
Class Staticmethod_demo (): role = ' Aray ' @staticmethod def func (): print (' When normal method is used ') staticmethod_ Demo.func () Output: When normal methods are used
2.classmethod: (class Method) class method: The default parameter is: CLS, can be called directly with the class name, you can interact with class properties.
Class Classmethod_demo (): role = ' Aray ' @classmethod def func (CLS): print (cls.role) classmethod_ Demo.func () output result: Aray
Common:
1. Can be directly called by the class, do not need to instantiate
Different points:
1, a class method must have a CLS parameter that represents this class, a static method that does not need to be used directly.
Binding methods: Common Method Class methods
Non-binding method: Static method
The methods learned so far are:
Common method: A Self object is passed in by default and can only be called by an object
Class method: By default, a CLS is passed in to represent this class, and can be called by classes and objects
Static methods: There are no default parameters and can be called by classes and objects. (This method is not recommended)
3.isinstance
The Isinstance function is to determine whether an object is an object of this class, pass two arguments, respectively, the object and the class, and, if it is, return true, otherwise, false.
Class Foo: passclass son (foo): passs = Son () #实例化print (Isinstance (S,son)) #判断一个对象是不是这个类的对象, pass two parameters, respectively Object and class print (Isinstance (S,foo)) print (type (s) = Son) #精准判断print (type (s) = = Foo) Output: Truetruetruefalse
4.issubclass
Issubclass: Determine if a class is a subclass of another class, passing two parameters, namely, subclass and parent class
Class Foo: passclass Son (Foo): passs = Son () #实例化print (Issubclass (Son,foo)) #判断一个类是不是另一个类的子类, pass two parameters , respectively, subclass and parent print (Issubclass (son,object)) print (Issubclass (Son,son)) print (Issubclass (foo,object)) Print (Issubclass ( Foo,son)) Output result: Truetruetruetruefalse
Second: Reflection
Reflection: Requires mastery (more on network-related aspects). Definition: You can use a string to access the object's properties and invoke the object's methods.
Common:
Hasattr:
GetAttr
Hasattr and getattr are usually used together. A detection, a fetch.
Class Black_one: feature = ' ugly ' def __init__ (self,name,addr): self.name = name SELF.AFFR = addr def sell_house (self): print ('%s ' The black intermediary sells the house, the idiot buys it, but who can prove that he is not a jerk? '%self.name) def rent_house (self): print ('%s Black intermediary introduction rent house, stupid only rent ah '%self.name) B1 = Black_one (' Heaven and earth three Clear ', ' Tao Impermanence ') # Detects if there is a property print (Hasattr (B1, ' name ')) print (Hasattr (B1, ' Sell_house ')) #获取属性n = GetAttr (B1, ' name ') print (n) func = GetAttr ( B1, ' Sell_house ') func () print (GetAttr (B1, ' aaaa ', ' not Present '))
Output Result:
True
True
Heaven and earth three Qing
Heaven and earth three clear black intermediary sell house, silly force just buy it, but who can prove that they are not stupid force?
does not exist
Not used:
SetAttr
#设置属性 (new) print (b1.__dict__) setattr (B1, ' SB ', True) SetAttr (B1, ' time ', ' 1 Years ') print (b1.__dict__)
Output: {' name ': ' Heaven and earth three clear ', ' addr ': ' Imitation of impermanence '}
{' Name ': ' Heaven and earth ', ' addr ': ' Tao impermanence ', ' SB ': True, ' time ': ' 1 Years '}
Delattr:
Delattr (B1, ' addr ') print (B1) output: {' name ': ' Heaven and earth three Clear ', ' b1.__dict__ ': ' Tao impermanence ', ' SB ': True, ' time ': ' 1 Years '} {' Name ': ' Heaven and earth three Clear ', ' SB ': True}
Built-in methods:
STR and REPR
Class Foo: def __init__ (self,name): self.name = name def __str__ (self): return '%s obj info in str ' %self.name def __repr__ (self): return ' obj info in repr ' F = Foo (' Egon ') print (f) print ('%s '%f) print ('%r '%f) Output Result: Egon obj info in Stregon obj info in strobj info in repr
-del-: If you do not delete it manually, the system will end up del by default.
Class Foo: def __del__ (self): print (' Execute me! ') F = Foo () print (123) print (123) print (123) del fprint (123) print (123) print (123) output: 123123123 Execute me! 123123123
Item Series:
Python = = Object-oriented reflection, (Isinstance and Issubclass)