Python tutorial dir () built-in functions and usage

Source: Internet
Author: User
Tags in python

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.

Related Article

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.