Dir () built-in functions
There are many built-in python methods. No beginner or experienced python programmers can remember all the methods. At this time, the dir () function is very useful. You can use the dir () function to view all objects and methods in the object. In python, everything is an object, a data type, a module, etc, all have their own attributes and methods. Except for common methods, you don't need to remember them all, just hand it over to the dir () function.
How to use the dir () function
The dir () function operation method is very simple. You only need to add the desired query and object to the brackets.
For example, if you want to view the methods of the list, you can directly input an empty list object in (), such as [] or a variable name of the list data type, as shown below:
>>> Dir ([])
Or
X = ['A', 'B']
>>> Dir (x)
The results of the two operation methods are the same. They are used to view the operation methods and attributes of the list. If you want to query the string, you only need to set the parameter variable name or null string ''in.
How to use the dir () function to view module attributes and methods
To view what a python module can do, first import the module, and then use the method described above to view it. For example, to view what the sys module can do, perform the following operations:
The dir () function is a common and useful function in Python. It returns a list of all attribute names of the object to be queried.