One, Isinstance (OBJ.CLS) & Issubclass (Sub,super)
Isinstance (OBJ.CLS): Used to check whether an object belongs to a class. (Whether an object is generated by a class)
L1 = List ()
Print Isinstance (l1,list)
Output:
True
2.issubclass (Sub,super): Used to determine whether a class is a subclass of another class.
#sub refers to subclasses, and Super refers to the parent class.
Class C1 (object):
Pass
Class C2 (C1):
Pass
Print Issubclass (C2,C1)
Output:
True
Two, __getattribute__ (built-in method)
This built-in approach, which is similar in name to __getattr__, but the conditions for triggering this built-in method are completely different from the __getattr__!
In an object, __getattr__ is triggered when the property or method you are looking for cannot be found.
__GETATTRBUTE__ will trigger this method regardless of whether the method or property can be found! Once this method is executed, if a Attributeerror exception is not triggered internally, then the __getattr__ method will never be triggered.
Here is an example:
class Class1 (object):
def __init__ (self,x):
self.x = X
def __getattr__ (self, item):
Print "Call __getattr__"
Obj1 = Class1 (123)
Print obj1.x
Output:
Call Test
# then call a non-existent property
obj1.asadsdsadasfasf
Output:
The call __getattr__ #__getattr__ method was successfully executed.
class Class1 (object):
def __init__ (self,x):
self.x = X
def __getattr__ (self, item):
Print "Call __getattr__"
def __getattribute__ (self, item):
Print "Call __getattribute__"
Raise Attributeerror ("^-^") # Throws a Attributeerror
Obj1 = Class1 (123)
obj1.x
Obj1.asasasasasas
Output execution Result:
Call __getattribute__
Call __getattr__
Call __getattribute__
Call __getattr__
From the above results, __getattribute__ when thrown a Attributeerror exception, __getattr__ will be triggered!!
This article is from the "Rebirth" blog, make sure to keep this source http://suhaozhi.blog.51cto.com/7272298/1917990
Complement 8.python Face Object Part.7 (supplementary to class-related functions)