Python object __***__ variable meaning description

Python object __***__ variable meaning description

Module

  • __doc__: Documentation string. If the module has no documentation, this value is None.
  • __name__: is always the name of the module when it is defined; even if you use import .. as to alias it or assign it to another variable name.
  • __dict__: Contains the attribute name-attribute dictionary available in the module; that is, the object that can be accessed using the module name and attribute name.
  • __file__: contains the file path of the module. Note that the built-in module does not have this attribute, and accessing it will throw an exception!

Class

  • __doc__: Documentation string. If the class has no documentation, this value is None.
  • __name__: Always the name of the class at the time of definition.
  • __dict__: Contains the attribute name-attribute dictionary available in the class; that is, the object that can be accessed using class name and attribute name.
  • __module__: The name of the module containing the definition of the class; note that it is the module name in the form of a string, not the module object.
  • __bases__: A tuple of direct parent class objects; but does not include other classes higher in the inheritance tree, such as the parent class of the parent class.
  • __mro__: Method resolution order; since Python supports multiple inheritance, if the parent class has the same method, the actual execution function will be selected in the order given by the tuple.

Instance

  • __dict__: Contains the available attribute name-attribute dictionary.
  • __class__: The class object of this instance. For class Cat, cat.__class__ == Cat is True.

Built-in functions and methods

  • __doc__: The documentation of the function or method.
  • __name__: The name of the function or method when it was defined.
  • __self__: Only the method is available. If it is bound, it points to the class (if it is a class method) or instance (if it is an instance method) that calls the method, otherwise it is None.
  • __module__: The name of the module where the function or method is located.

Function

  • __doc__: The documentation of the function; you can also use the attribute name func_doc.
  • __name__: The name of the function when the function is defined; in addition, the attribute name func_name can also be used.
  • __module__: The name of the module that contains the function definition; also note that it is the module name, not the module object.
  • __dict__: Available attributes of the function; in addition, the attribute name func_dict can also be used. 
    Don't forget that functions are also objects. You can use function.attribute name to access attributes (if the attribute does not exist when assigning a value, one will be added), or use the built-in function has/get/setattr() to access. However, it doesn't make much sense to store attributes in functions.
  • func_defaults: This attribute saves the parameter default value tuple of the function; because the default value is always the last parameter, it can correspond to the parameter without using a dictionary.
  • func_code: This attribute points to a code object corresponding to the function, and some other special attributes are defined in the code object, which will be introduced separately below.
  • func_globals: This attribute points to the current global namespace instead of the global namespace when the function is defined. It is not very useful and is read-only.
  • func_closure: This attribute is only valid when the function is a closure. It points to a tuple that holds the variable cell of the referenced external function. If the function is not an internal function, it is always None. This attribute is also read-only.

Method

  • __doc__: Same as function.
  • __name__: Same as function.
  • __module__: Same as function.
  • im_func: Use this attribute to get a reference to the actual function object in the method. In addition, if it is a version above 2.6, the attribute name __func__ can also be used.
  • im_self: If it is bound, it points to the class (if it is a class method) or instance (if it is an instance method) that calls the method, otherwise it is None. If it is a version above 2.6, the attribute name __self__ can also be used.
  • im_class: The class that actually calls the method, or the class of the instance that actually calls the method. Note that it is not the class where the method is defined, if there is an inheritance relationship.

Generator

  • __iter__: is just an iterable mark.
  • gi_code: The code object corresponding to the generator.
  • gi_frame: The frame object corresponding to the generator.
  • gi_running: Whether the generator function is executing. The generator function is in the frozen state after the yield and before the next line of code is executed. At this time, the value of this attribute is 0.
  • next|close|send|throw: These are several callable methods that do not contain metadata information. You can check the relevant documentation of the generator for how to use it.

Code block (code)

  • co_argcount: The total number of ordinary parameters, excluding *parameters and **parameters.
  • co_names: tuple of all parameter names (including *parameters and **parameters) and local variable names.
  • co_varnames: A tuple of all local variable names.
  • co_filename: The name of the file where the source code is located.
  • co_flags: This is a value, each binary bit contains specific information. More attention is paid to 0b100 (0x4) and 0b1000 (0x8). If co_flags & 0b100 != 0, the *args parameter is used; if co_flags & 0b1000 != 0, the **kwargs parameter is used. In addition, if co_flags & 0b100000(0x20) != 0, it means that this is a generator function.

Stack frame

The stack frame represents a certain frame in the function call stack when the program is running. The function has no attribute to get it, because it is only generated when the function is called, and the generator is returned by the function call, so there is an attribute pointing to the stack frame. If you want to get the stack frame related to a function, you must get it when the function is called and the function has not yet returned. You can use the _getframe() function of the sys module or the currentframe() function of the inspect module to get the current stack frame. The attributes listed here are all read-only.

  • f_back: The previous frame of the call stack.
  • f_code: The code object corresponding to the stack frame.
  • f_locals: When used in the current stack frame, it is the same as the built-in function locals(), but you can get other frames first and then use this property to get the locals() of that frame.
  • f_globals: When used in the current stack frame, it is the same as the built-in function globals(), but you can get other frames first...

Traceback

Tracking is an object used to backtrack when an exception occurs, as opposed to a stack frame. Since it will be constructed when an exception occurs, and it will always be thrown to the outer stack frame when the exception is not caught, you need to use try to see this object. You can use the exc_info() function of the sys module to get it. This function returns a tuple, the elements are exception type, exception object, and trace. The attributes of traceback are all read-only.

  • tb_next: The next tracked object to track.
  • tb_frame: The stack frame corresponding to the current track.
  • tb_lineno: The line number currently being tracked.

Use inspect module

Check object type

  • is{module|class|function|method|builtin}(obj): 
    Check whether the object is a module, class, function, method, built-in function or method.
  • isroutine(obj): 
    Used to check whether the object is a callable type such as a function, method, built-in function or method. This method is more convenient than multiple is*(), but its implementation still uses multiple is*(). For class instances that implement __call__, this method will return False. If the purpose is to be True as long as it can be called directly, you may wish to use isinstance(obj, collections.Callable). I don’t know why Callable is in the collections module, sorry! I'm probably because the collections module contains many other ABC (Abstract Base Class) reasons:)

Get object information

  • getmembers(object[, predicate]): 
    This method is an extended version of dir(), it will return the attributes corresponding to the name found by dir() together, like [(name, value), ...]. In addition, predicate is a method reference. If specified, it should accept value as a parameter and return a boolean value. If it is False, the corresponding property will not be returned. Use is* as the second parameter to filter out the attributes of the specified type.
  • getmodule(object): 
    Still regretting that the __module__ attribute in Section 2 only returns a string? This method must satisfy you, it returns the module object where the definition of object is located.
  • get{file|sourcefile}(object): 
    Get the file name|source code file name of the module where the definition of object is located (if not, return None). TypeError will be thrown when used on built-in objects (built-in modules, classes, functions, methods).
  • get{source|sourcelines}(object): 
    Get the source code of the definition of object and return it as a string|string list. IOError will be thrown when the code cannot be accessed. Can only be used for module/class/function/method/code/frame/traceack objects.
  • getargspec(func): 
    only used for methods, get the parameters declared by the method, and return tuples, which are (list of ordinary parameter names, *parameter names, **parameter names, default value tuples). If there is no value, it will be an empty list and 3 None. If it is version 2.6 or higher, a Named Tuple will be returned, that is, in addition to the index, you can also use the attribute name to access the elements in the tuple.  
  • getargvalues(frame): 
    only used for stack frames, get the parameter values ​​of the function call saved in the stack frame, and return a tuple, which are (list of common parameter names, *parameter names, **parameter names, locals of the frame) ()). If it is version 2.6 or higher, a Named Tuple will be returned, that is, in addition to the index, you can also use the attribute name to access the elements in the tuple. 
  • getcallargs(func[, *args][, **kwds]): 
    Returns a dictionary of the corresponding values ​​of each parameter when calling this method with args and kwds. This method is only available in version 2.7.
  • getmro(cls): 
    returns a type tuple, in accordance with the order in this tuple when searching for class attributes. If it is a new-style class, the result is the same as cls.__mro__. But the old-style class does not have the __mro__ attribute, and using this attribute directly will report an exception, so this method still has its value. Returns the current stack frame object.

Guess you like

Origin blog.csdn.net/a40850273/article/details/98480126