1, Issubclass ()
The Issubclass () Boolean function determines whether a class is a subclass or descendant class of another class.
[Python]View Plaincopy
- Issubclass (Sub, SUP)
Issubclass () returns True if the given sub-class sub determines to be a subclass of the parent sup (or False, conversely). This function also allows "not strict" subclasses, meaning that a class can be considered a subclass of itself. The second argument to Issubclass () can be a tuple (tuple) of possible parent classes, which returns True whenever the first argument is a subclass of any of the candidate classes in a given tuple.
2, Isinstance ()
Isinstance () Boolean functions are useful when judging whether an object is an instance of another given class.
[Python]View Plaincopy
- Isinstance (Obj1, Class)
Isinstance () returns True (or False) when Obj1 is an instance of class classes, or is an instance of a subclass of class.
[Python]View Plaincopy
- >>> class C1:
- ... Pass
- ...
- >>> class C2:
- ... Pass
- ...
- >>> C1 = C1 ()
- >>> C2 = C2 ()
- >>> isinstance (C1, C1)
- True
- >>> isinstance (C2, C1)
- False
- >>> isinstance (C1, C2)
- False
- >>> isinstance (C2, C2)
- True
- >>> isinstance (C2, C2)
- Traceback (most recent):
- File "<stdin>", line 1, in <module>
- Typeerror:isinstance () Arg 2 must be a type or tuple of types
Note: The second parameter should be a class; otherwise, you will get a typeerror.
As with Issubclass (), isinstance () can also use a tuple as the second parameter. Returns True if the first argument is an instance of any of the candidate types or classes of a given tuple in the second argument.
3, Hasattr (), GetAttr (), SetAttr (), delattr ()
When these functions are used, the object being processed is passed as the first parameter, but the property name, which is the second parameter of these functions, is the string name of those properties.
The purpose of hasattr () is to determine whether an object has a specific property, which is typically used to check a property before accessing it. The GetAttr () and SetAttr () functions acquire and assign properties to the object accordingly, and GetAttr () throws a Attributeerror exception when you attempt to read a nonexistent property, unless the optional default argument is given. SetAttr () will either add a new attribute or replace one that already exists. The delattr () function removes the property from an object.
[Python]View Plaincopy
- >>> class MyClass:
- ... def __init__ (self):
- ... Self.foo =
- ...
- >>> myinst = MyClass ()
- >>> hasattr (myinst, ' foo ')
- True
- >>> getattr (myinst, ' foo ')
- 100
- >>> hasattr (myinst, ' bar ')
- False
- >>> getattr (myinst, ' bar ')
- Traceback (most recent):
- File "<stdin>", line 1, in <module>
- Attributeerror: ' MyClass ' object has no attribute ' bar '
- >>> getattr (myinst, ' bar ', ' oops! ')
- ' Oops! '
- >>> setattr (myinst, ' bar ', ' my attr ')
- >>> getattr (myinst, ' bar ')
- ' My attr '
- >>> delattr (myinst, ' foo ')
- >>> hasattr (myinst, ' foo ')
- False
4. Dir ()
Use DIR () to list information about all the properties of a module.
A. Dir () shows instance variables when acting on an instance, as well as methods and class properties defined in the class where the instance resides and all its base classes
B. Dir () when acting on a class, displays the contents of the class and the __dict__ of all its base classes.
C. Dir () when acting on a module, displays the contents of the module's __dict__.
D. Dir () displays the local variables of the caller when the parameter is not present.
5. Super ()
The purpose of this function is to help the programmer to find the corresponding parent class, and then make it easy to invoke the related property. Super () Returns a Super object, which is responsible for parsing the method. When a rhetorical question is asked, he looks for all the superclass (and superclass superclass) until the desired attribute is found.
6, VARs ()
VARs () returns a dictionary that contains the properties (keys) and values that the object stores in its __dict__.
[Python]View Plaincopy
- >>> class c:
- ... PASS&NBSP;&NBSP;
- ...&NBSP;&NBSP;
- >>> c = c ()
- >>> c.foo = 100&NBSP;&NBSP;
- >>> c.bar = ' Python '
- >>>&NBSP;C.__DICT__&NBSP;&NBSP;
- { ' foo ': 100, ' bar ': ' Python '}
- >>> vars (c)
- { ' foo ': 100, ' Python '}
If the object is not provided as a parameter to VARs (), it displays a dictionary of properties (keys) and their values that contain the local namespace, that is, locals ().
Python built-in functions