Recently really is some busy, just after years, accumulated a lot of things to deal with, so the daily function can only be updated every two days, here and everyone apologizes.
Today we look at a very important function: Dir ()
英文说明: Returns a list of the types of variables, methods, and definitions in the current scope without parameters, and returns a list of properties and methods for parameters when they are with parameters. If the parameter contains method __dir__ (), the method is called. If the parameter does not contain __dir__ (), the method will collect the parameter information to the fullest extent.
Parameter: Object, variable, type.
Version: This function is available in all versions of Python, but the details of the attributes displayed in each version are different. Pay attention to the differences when using.
English Description:
Dir ([Object])
Without arguments, return the list of names in the current local scope. With an argument, attempt to return a list of valid attributes for that object.
If the object has a method named __dir__ (), this method would be called and must return the list of attributes. This allows objects, implement a custom __getattr__ () or __getattribute__ () function to customize the by Dir () report s their attributes.
If The object does not provide __dir__ (), the function tries its best to gather information from the object ' s __dict__ att Ribute, if defined, and from its type object. The resulting list is not necessarily complete, and could be inaccurate if the object has a custom __getattr__ ().
The default dir () mechanism behaves differently with different types of objects, as it attempts-produce the most Releva NT, rather than complete, information:
If the object is a module object, the list contains the names of the module ' s attributes.
If The object is a type or class object, the list contains the names of its attributes, and recursively of the attributes of its bases.
Otherwise, the list contains the object ' s attributes ' names, the names of its class ' s attributes, and recursively of the A Ttributes of its class ' s base classes.
The resulting list is sorted alphabetically. For example:
>>> Import struct>>> dir () # Show the names in the module namespace[' __builtins__ ', ' __doc__ ', ' __na me__ ', ' struct ']>>> dir (struct) # show the names in the struct module[' struct ', ' __builtins__ ', ' __doc__ ', ' _ _file__ ', ' __name__ ', ' __package__ ', ' _clearcache ', ' calcsize ', ' Error ', ' pack ', ' pack_into ', ' unpack ', ' Unpack_from ']& Gt;>> class Shape (object): def __dir__ (self): return ["area", ' perimeter ', ' location ']>>> s = Shape () >>> dir (s)
[' Area ', ' perimeter ', ' location ']
Note Because dir () is supplied primarily as a convenience for use in an interactive prompt, it tries-supply an interest ing set of names more than it tries to supply a rigorously or consistently defined set of names, and its detailed behavior may change across releases. For example, Metaclass attributes was not in the result list, and the argument is a class.
Special Note: Change series article all code instances No special instructions are based on the python2.7
code example:
>>> dir () [' __builtins__ ', ' __doc__ ', ' __name__ ', ' __package__ ']>>> import struct>>> dir () [ ' __builtins__ ', ' __doc__ ', ' __name__ ', ' __package__ ', ' struct ']>>> dir (struct) [' struct ', ' __builtins__ ', ' __ Doc__ ', ' __file__ ', ' __name__ ', ' __package__ ', ' _clearcache ', ' calcsize ', ' Error ', ' pack ', ' pack_into ', ' unpack ', ' Unpack_from ']>>> class Person (object): ... def __dir__ (self): ... return ["Name", "Age", "Country"]...>>> dir (person) [' __class__ ', ' __delattr__ ', ' __dict__ ', ' __dir__ ', ' __doc __ ', ' __format__ ', ' __getattribute__ ', ' __hash__ ', ' __init__ ', ' __module__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __setattr__ ', ' __sizeof__ ', ' __str__ ', ' __subclasshook__ ', ' __weakref__ ']>>> tom = person () > >> dir (Tom) [' Age ', ' country ', ' name ']