Isinstance (obj,cls) checks if obj is an object of the class CLS.
Isinstance (obj,cls) checks if obj is an object of the class CLS.
Reflection
Python-oriented reflection in objects: manipulating Object-related properties in the form of strings. All things in Python are objects (you can use reflection)
Four functions that can be self-reflective (parameter is object, property name in string format)
Hasattr
GetAttr
SetAttr
Delattr
__del__, which is the destructor method, automatically triggers execution when the object is freed in memory.
Note: This method is generally not defined because Python is a high-level language, and programmers do not need to be concerned with allocating and releasing memory because this work is done by the Python interpreter, so the destructor calls are automatically triggered by the interpreter when it is garbage collected. There are no specialized constructs and destructors in Python, but they can be initialized and deleted separately in __init__ and __del__, and this alternative constructs and destructors are available
__getitem__
__getitem__ the particularity of this method with double underline
In the example at the beginning of this article, if the class defines the __getitem__ method, when the class is sliced, (in the format of the object name [' key '], the run of the __getitem__ is triggered), the slice is passed in as a __getitem__ parameter, You can also get two values to a slice by using the start and stop methods.
In Python you want to make instances of classes use subscript like List, you can use the __getitem__ method, and with slice objects you can make list-like slices
def __setitem__ (Self,key,value)
return SetAttr (Self,key,value)
def __delitem__ (Self,key)
Return Delattr (Self,key)
__str__
First, Description:
__INIT__: class instance initialization function
__STR__: class instance string function
Two
Example 1:
Class friend () Span class= "Hljs-class" >: def __init__ (self, name) : self.name = nameif __name_ _ == ' __main__ ': friend = friend ( ' Liang ') print friend
Output result: <__main__. Friend instance at 0x1d92a70>
The result of the above is the storage address, how to print out the content of an object's thoughts? You can pass the __STR__ function
Example 2:
ClassFriend():Def__init__(Self, name): Self.name = Namedef __str__(self): return "freind:%s"%self.nameif __name__ = = ' __main__ ': Friend = Friend (' Liang ') print friend
Output Result: Friend:liang
Python Object-oriented advanced