Is there a helper function in python

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

Is there a helper function in python
Is there an algorithm in python language
What is an anonymous function in Python
What is a sequence table in Python
Is a number in python a variable type
What is the function of adb in python
What is introspection in python
What is object-oriented in python
What is list comprehension in python
Python is a cross-platform language code
There are several keywords in Python
Everything in Python is an object
Write a Qixi confession artifact in Python
How to run id function in python
What is the id function of python
How to sort a dictionary in python
Python is mainly used in which directions
How to use the round function in python
Python 3.9 is here!
Functions in python
How to use the zip function in Python
How to simulate gravity in a Python game
How to use the format function in python
Install Python3 environment in a brand new Ubuntu
Python enumerate() function
How to write a confession program in python
Python function buffer
How to understand a list of numbers in python
How to create a Python virtual environment in Ubuntu 14.04
Python implements FTP to upload files in a loop
Why python is popular
Python is short-crawling music
03. Operators in Python entry
Python is slowly fading
Python custom function basics
12. Network Programming in Python3
print statement in python
Python built-in function -compile()
Python function basic learning
Python data analysis-apply function
Python3 built-in function table.md
Concurrent requests in Python
Install python in Ubuntu
Write gui in python
Python Print print timer function
MongoDB usage in Python
Str string in Python
Computational Geometry in Python
How to find the area of a circle in python
A large inventory of commonly used third-party libraries in Python