The dir() function in python is a very important function, it can help us view the function and characteristics of the function.
Chinese description: without parameters, return the list of variables, methods and defined types in the current scope; when with parameters, return the list of parameter attributes and methods. If the parameter contains the method dir(), the method will be called. If the parameter does not contain dir(), this method will collect parameter information as much as possible.
Parameters object: object, variable, type.
Version: This function is available in all versions of python, but the attribute details displayed in each version are different. Pay attention to the difference when using.
E.g
import struct
dir() # show the names in the module namespace
['__ builtins__','__doc__','__name__','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']classShape(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 at an interactive prompt, it tries
to supply an
interesting setof names more than it tries to supply a rigorously or consistently defined setof
names, and its
detailed behavior may change across releases. For example, metaclass attributes are not in the result
list when the
argument is a class.
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']classPerson(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']
Knowledge point expansion:
The role of the help() function
When using python to write code, you often use python's own functions or modules. The purpose of some uncommon functions or modules is not very clear. At this time, you need to use the help function to view the help.
It should be noted here that the help() function is to view the detailed description of the purpose of the function or module, and the dir() function is to view the operation methods in the function or module, and the output is a list of methods.
How to use the help function to view the usage of functions in the python module
Help() fill in the parameters in parentheses, the operation method is very simple. E.g:
help('dir')
Help on built-infunction dir in module builtins:dir(...)dir([object])- list of strings
If called without an argument,return the names in the current scope.
Else,return an alphabetized list of names comprising(some of) the attribut
es
of the given object, and of attributes reachable from it.
If the object supplies a method named __dir__, it will be used; otherwise
the defaultdir() logic is used and returns:for a module object: the module's attributes.for a classobject: its attributes, and recursively the attributes
of its bases.for any other object: its attributes, its class's attributes, and
recursively the attributes of its class's base classes.
This is the end of this article on whether there are help functions in python. For more detailed explanations of related python help functions, please search for the previous articles of ZaLou.Cn or continue to browse the related articles below. Hope you will support ZaLou.Cn more in the future. !
Recommended Posts