Python introspection Guide

Source: Internet
Author: User
Document directory
  • 2.0. Preparation: determine the object type
  • 2.1. Module)
  • 2.2. Class)
  • 2.3. Instance)
  • 2.4. Built-in functions and methods)
  • 2.5. Functions)
  • 2.6. Method)
  • 2.7. Generator)
  • 2.8. Code Block)
  • 2.9. stack frame)
  • 2.10. Trace (traceback)
  • 3.1. Check the object type
  • 3.2. Get Object Information

In my own concepts, introspection and reflection are the same thing. Of course, I am not very sure and sure, so if these two concepts are indeed different, for more information, see the author, source, and link to the original article. Thank you!
Update 2011-3-10: Correct the meaning of the func_globals attribute of the function.

First, let's look at the objects and related concepts that may be used in this article through an example.

# Coding: UTF-8import sys # module, sys points to this module object import inspectdef Foo (): pass # function, foo points to this function object class CAT (object): # class, cat points to this class Object def _ init _ (self, name = 'Kitty '): Self. name = Name def sayhi (Self): # instance method. sayhi points to this method object and uses a class or instance. sayhi accesses print self. name, 'says hi! '# Access the field named name and use the instance. name access cat = CAT () # Cat is the cat class instance object print cat. sayhi # when using the class name to access the instance method, the method is unbound (unbound) print cat. sayhi # when using the instance access method, the method is bound (bound)

Sometimes we need to execute a method of the object or assign a value to a field of the object. The method name or field name cannot be determined when coding the code, you must input a string using parameters. For example, to implement a common dBm framework, you may need to assign values to the fields of the data object, however, we cannot predict the fields of data objects that use this framework. In other words, when writing a framework, we need to access unknown attributes through some mechanism.

This mechanism is called reflection (which in turn allows an object to tell us what it is) or introspection (letting the object tell us what it is, okay, I admit that I am lying in the brackets---#), used to get information about unknown objects at runtime. Reflection is a very scary term. It sounds uncertain. reflection in a general programming language is a little more complex than other concepts. Generally, reflection is a high-level topic; however, reflection in python is very simple, and it hardly feels different from other code. The functions and methods obtained by reflection can be directly called with parentheses as usual, the instance can be constructed directly after the class is obtained. However, the obtained field cannot be assigned a value directly, because another reference pointing to the same place is obtained, and the value assignment can only change the current reference.

1. Access Object Attributes

The following lists several built-in methods that can be used to check or access the attributes of an object. These methods can be used for any object, not just the cat instance object in the example; everything in python is an object.

Cat = CAT ('Kitty ') print cat. name # access instance attributes cat. sayhi () # Call the instance method print Dir (CAT) # obtain the instance attribute name and return if hasattr (CAT, 'name') in the form of a list '): # Check whether the instance has this property setattr (CAT, 'name', 'tiger ') # Same as:. name = 'tider' print getattr (CAT, 'name') # Same as: Print. namegetattr (CAT, 'sayhi') () # Same as: cat. sayhi ()
  • Dir ([OBJ]):

    Calling this method will return a list containing most of the OBJ attribute names (some special attributes are not included ). The default value of obj is the current module object.
  • Hasattr (OBJ, ATTR ):

    This method is used to check whether OBJ has an attribute named ATTR and returns a Boolean value.
  • Getattr (OBJ, ATTR ):

    Calling this method will return the attribute value named ATTR in OBJ. For example, if ATTR is 'bar', obj. Bar is returned.
  • Setattr (OBJ, ATTR, Val ):

    To call this method, the attribute named ATTR of obj is assigned Val. For example, if ATTR is 'bar', it is equivalent to OBJ. bar = Val.
2. Access object metadata

When you use Dir () for a constructed object, you may find that many attributes in the list are not defined by you. These attributes generally Save the metadata of the object. For example, the _ name _ attribute of the class stores the class name. Most of these attributes can be modified, but they do not make much sense. modify some of these attributes, such as function. func_code can also cause problems that are difficult to find, so you can just change the name or something. Do not modify other attributes without knowing the consequences.

Next, we will list some special attributes of a specific object. In addition, some attributes mentioned in the python document may not always be provided.*Mark, you can open the interpreter to confirm before use.

2.0. Preparation: determine the object type

All Python built-in types are defined in the types module. The specific object types can be determined by using the built-in method isinstance.

  • Isinstance (object, classinfo ):

    Check whether the object is the type listed in classinfo and return a Boolean value. Classinfo can be a specific type, or multiple types of tuples or lists.

The types module only defines types, while the inspect module encapsulates many methods for checking types, which is easier than directly using the types module. Therefore, we will not provide more information about types here, if necessary, you can directly view the document description of the types module. The inspect module is described in Section 3rd.

2.1. Module)
  • _ Doc __: document string. If the module does not have a document, the value is none.
  • *_ Name __: always the module name during definition, even if you use import... as to get an alias for it or assign a value to another variable name.
  • *_ Dict __: contains the 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. An exception is thrown when you access it!
import fnmatch as mprint m.__doc__.splitlines()[0] # Filename matching with shell patterns.print m.__name__                # fnmatchprint m.__file__                # /usr/lib/python2.6/fnmatch.pycprint m.__dict__.items()[0]     # ('fnmatchcase', 
 
  )
 
2.2. Class)
  • _ Doc __: document string. If the class does not have a document, the value is none.
  • *_ Name __: always the class name during definition.
  • *_ Dict __: contains the attribute dictionary available in the class, that is, the object that can be accessed using the class name. attribute name.
  • _ Module __: the module name that contains the definition of this class. Note that it is a module name in the string format, not a module object.
  • *_ Bases __: the tuples of the parent class object, but does not contain other classes on the upper layer of the inheritance tree, such as the parent class of the parent class.
print Cat.__doc__           # Noneprint Cat.__name__          # Catprint Cat.__module__        # __main__print Cat.__bases__         # (
 
  ,)print Cat.__dict__          # {'__module__': '__main__', ...}
 
2.3. Instance)

An instance is an object after the class is instantiated.

  • *_ Dict __: contains the available attribute name-attribute dictionary.
  • *_ Class __: Class Object of the instance. For cat classes, Cat. _ class _ = cat is true.
print cat.__dict__print cat.__class__print cat.__class__ == Cat # True
2.4. Built-in functions and methods)

According to the definition, the built-in (built-in) module refers to the module written in C. You can check which modules are built in through the builtin_module_names field of the SYS module. The functions and methods in these modules have fewer attributes, but they generally do not need to be viewed in the code.

  • _ Doc __: function or method document.
  • _ Name __: the name used to define a function or method.
  • _ Self __: only the method is available. If it is bound (bound), it points to the class (if it is a class method) or instance (if it is an instance method) that calls this method ), otherwise, the value is none.
  • *_ Module __: name of the module where the function or method is located.
2.5. Functions)

This is a non-built-in function. Note that Def is used in classes to define methods. Methods and functions have similar behaviors, but they are different concepts.

  • _ Doc __: function documentation. You can also use the attribute name func_doc.
  • _ Name __: name of the function when defining the function. You can also use the attribute name func_name.
  • *_ Module __: contains the module name defined by the function. also note that it is a module name rather than a module object.
  • *_ Dict __: available attribute of the function. You can also use the attribute name func_dict.

    Do not forget that the function is also an object. You can use the function. attribute name to access the attribute (if the attribute does not exist, a new one will be added), or use the built-in function has/get/setattr () for access. However, saving attributes in a function is of little significance.
  • Func_defaults: This attribute stores the default value tuples of the function parameters. Because the default values are always dependent on the backend parameters, the dictionary format can also correspond to the parameters.
  • Func_code: This attribute points to the Code object corresponding to the function. The Code object defines some other special attributes, which will be described below.
  • Func_globals: This attribute points to the global namespace when the function is defined.
  • *Func_closure: This attribute is valid only when the function is a closure and points to the cell variable that stores the referenced external function. If the function is not an internal function, it is always none. This attribute is also read-only.

The following code demonstrates func_closure:

# Coding: UTF-8def Foo (): n = 1 def bar (): Print N # reference non-global external variable n to construct a closure n = 2 Return barclosure = Foo () print closure. func_closure # Use Dir () to know that the cell object has a cell_contents attribute and can obtain the print closure value. func_closure [0]. cell_contents #2

In this example, we can see that using Dir () for unknown objects is a good idea :)

2.6. Method)

Although the method is not a function, it can be understood that a shell is added to the function. After obtaining the actual function in the method, you can use the attribute in section 2.5.

  • _ Doc __: same as the function.
  • _ Name __: same as the function.
  • *_ Module __: same as the function.
  • Im_func: You can use this attribute to obtain the reference of the actual function object in the method. In addition, for Versions later than 2.6, you can also use the attribute name _ FUNC __.
  • Im_self: If it is bound (bound), it points to the class (if it is a class method) or instance (if it is an instance method) that calls this method, otherwise it is none. For Versions later than 2.6, you can also use the attribute name _ self __.
  • Im_class: the class that actually calls this method, or the class of the instance that actually calls this method. Note that it is not the class where the method definition is located, if there is an inheritance relationship.
im = cat.sayHiprint im.im_funcprint im.im_self # catprint im.im_class # Cat

The general instance method is discussed here. There are two special methods, classmethod and staticmethod ). Class method or method, but it is always bound because it needs to be called by class name; the static method can be regarded as a function in the namespace of the class (a function called by the class name is required). It can only use the attributes of the function, but cannot use the attributes of the method.

2.7. Generator)

A generator is an object returned by calling a generator function. It is mostly used for the iteration of a collection object.

  • _ ITER __: it is just an iterative tag.
  • 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 being executed. The generator function is in the frozen state after yield and before the next line of yield code is executed. The value of this attribute is 0.
  • Next | close | send | throw: This is a few callable methods that do not contain metadata. You can view the relevant documentation of the generator for how to use it.
def gen():    for n in xrange(5):        yield ng = gen()print g             # <generator object gen at 0x...>print g.gi_code     # <code object gen at 0x...>print g.gi_frame    # <frame object at 0x...>print g.gi_running  # 0print g.next()      # 0print g.next()      # 1for n in g:    print n,        # 2 3 4

Next we will discuss several built-in object types that are not frequently used. These types should be rarely used during normal encoding, unless you are implementing an interpreter or development environment on your own. Therefore, only some attributes are listed here. If you need a complete Attribute Table or want to learn more, you can view the reference documents listed at the end of this article.

2.8. Code Block)

The code block can be compiled by the class source code, function source code, or a simple statement code. Here, we only consider how it refers to a function. We mentioned in section 2.5 that it can be obtained using the func_code attribute of the function. All the Code attributes are read-only.

  • Co_argcount: Total number of common parameters, excluding the * parameter and ** parameter.
  • Co_names: tuples of all parameter names (including * parameter and ** parameter) and local variable names.
  • Co_varnames: The tuples of all local variable names.
  • Co_filename: name of the source code.
  • Co_flags: This is a numerical value. Each binary BIT contains specific information. Note 0b100 (0x4) and 0b1000 (0x8) If co_flags & 0b100! = 0 indicates that the * ARGs parameter is used. If co_flags & 0b1000! = 0. The ** kwargs parameter is used. In addition, if co_flags & 0b100000 (0x20 )! = 0 indicates that this is a generator function ).
co = cat.sayHi.func_codeprint co.co_argcount        # 1print co.co_names           # ('name',)print co.co_varnames        # ('self',)print co.co_flags & 0b100   # 0
2.9. stack frame)

Stack frame indicates a frame in the function call stack when the program is running. A function has no attribute to obtain it, because it is generated only when the function is called, and the generator is returned by the function call. Therefore, the attribute points to the stack frame. To obtain stack frames related to a function, you must obtain them when calling this function and the function has not yet been returned. You can use the _ getframe () function of the SYS module or the currentframe () function of the inspect module to obtain the current stack frame. All the attributes listed here are read-only.

  • F_back: the previous frame of the call stack.
  • F_code: the code object corresponding to the stack frame.
  • F_locals: Used in the current stack frame is the same as the built-in function locals (), but you can get other Frames first and then use this attribute to get the locals () of that frame ().
  • F_globals: The current stack frame is the same as the built-in function globals (), but you can get other Frames first .......
def add(x, y=1):    f = inspect.currentframe()    print f.f_locals    # same as locals()    print f.f_back      # <frame object at 0x...>    return x+yadd(2)
2.10. Trace (traceback)

Tracing is an object used for backtracking when an exception occurs, which is opposite to stack frames. This object is constructed only when an exception occurs, but is always thrown to the outer stack frame when the exception is not captured. Therefore, you need to use try to see this object. You can use the exc_info () function of the SYS module to obtain it. This function returns a tuples with the exception type, exception object, and tracing elements. All traceback attributes are read-only.

  • Tb_next: The next tracing object.
  • Tb_frame: stack frame corresponding to the current tracing.
  • Tb_lineno: the row number of the current trail.
Def Div (x, y): Try: Return x/y counter T: TB = sys. exc_info () [2] # Return (exc_type, exc_value, traceback) print TB. tb_lineno # "Return x/y" row number Div (1, 0)
3. Use the inspect Module

The inspect module provides a series of functions to help with introspection. The following lists some commonly used functions. For more information about the functions, see the inspect module documentation.

3.1. Check the 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 ):

    It is used to check whether an object is a function, method, built-in function, or method call type. This method is more convenient than multiple is * (), but its implementation still uses multiple is *().
    im = cat.sayHiif inspect.isroutine(im):    im()

    For a class instance that implements _ call _, this method returns false. If it is required to be true if it can be called directly, useIsinstance (OBJ, collections. callable)This form. I don't know why callable will be in the collections module. Sorry! I guess it is because the collections module contains many other ABC (abstract base class :)

3.2. Get Object Information
  • Getmembers (object [, predicate]):

    This method is an extended version of Dir (). It returns the attributes corresponding to the name found in Dir (), such as [(name, value),...]. In addition, predicate is a reference to a method. If it is specified, value should be accepted as a parameter and a Boolean value should be returned. If it is false, the corresponding attribute will not be returned. Use is * as the second parameter to filter out attributes of the specified type.
  • Getmodule (object ):

    Is the _ module _ attribute in section 2nd still returning only strings, but sorry? This method can satisfy your needs. It returns the module object of the object definition.
  • Get {file | sourcefile} (object ):

    Get the file name of the module where the object definition is located | source code file name (if not, none is returned ). A typeerror exception is thrown when it is used on built-in objects (built-in modules, classes, functions, and methods.
  • Get {source | sourcelines} (object ):

    Obtains the source code defined in the object and returns it with a string | string list. An ioerror occurs when the Code cannot be accessed. It can only be used for module/class/function/method/code/frame/traceack objects.
  • Getargspec (func ):

    It is only used for methods to obtain the parameters declared by the method. The returned tuples are (list of common parameter names, * parameter names, ** parameter names, and default value tuples ). If there is no value, it will be an empty list and three none. If the version is 2.6 or later, a named tuple is returned, that is, in addition to the index, attribute names can also be used to access the elements in the tuples.
    def add(x, y=1, *z):    return x + y + sum(z)print inspect.getargspec(add)#ArgSpec(args=['x', 'y'], varargs='z', keywords=None, defaults=(1,))
  • Getargvalues (FRAME ):

    Used only for Stack frames. Get the parameter values of the function call stored in the stack frames. The returned tuples are (list of common parameter names, * parameter names, ** parameter names, the locals () of the frame ()). If the version is 2.6 or later, a named tuple is returned, that is, in addition to the index, attribute names can also be used to access the elements in the tuples.
    def add(x, y=1, *z):    print inspect.getargvalues(inspect.currentframe())    return x + y + sum(z)add(2)#ArgInfo(args=['x', 'y'], varargs='z', keywords=None, locals={'y': 1, 'x': 2, 'z': ()})
  • Getcallargs (func [, * ARGs] [, ** kwds]):

    Returns the dictionary of values corresponding to each parameter when ARGs and kwds call this method. This method is available only in version 2.7.
  • Getmro (CLS ):

    A type tuples are returned, which are sorted by class attributes. For new classes, the results are the same as those of Cls. _ Mro. However, the old class does not have the _ Mro _ attribute. Using this attribute directly will report an exception, so this method still has its value.
    print inspect.getmro(Cat)#(<class '__main__.Cat'>, <type 'object'>)print Cat.__mro__#(<class '__main__.Cat'>, <type 'object'>)class Dog: passprint inspect.getmro(Dog)#(<class __main__.Dog at 0x...>,)print Dog.__mro__ # AttributeError
  • Currentframe ():

    Returns the current stack frame object.

For details about other frame and traceback functions, refer to the inspect module documentation, which is rarely used.

<Full text>

References:

1. The standard type hierarchy [official documentation] [English]

2. Inspect-inspect live objects [official documentation] [English]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.