Python function-dir ()

Source: Internet
Author: User
Tags list of attributes
The python function-dir () has been a little busy recently. after just a year, many things have been accumulated and need to be processed. Therefore, a function can only be updated every two days, I apologize to you here.

Today, let's look at a very important function: dir ()

Chinese description: If no parameter is included, a list of variables, methods, and definitions in the current range is returned. if a parameter is included, a list of attributes and methods of the parameter is returned. If the parameter contains method _ dir _ (), the method is called. If the parameter does not contain _ dir _ (), this method collects parameter information to the maximum extent.

Parameter 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 differences during use.

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 will be called and must return the list of attributes. this allows objects that implement a custom _ getattr _ () or _ getattribute _ () function to customize the way dir () reports their attributes.


If the object does not provide _ dir _ (), the function tries its best to gather information from the object's _ dict _ attribute, if defined, and from its type object. the resulting list is not necessarily complete, and may be inaccurate when the object has a custom _ getattr __().


The default dir () mechanic behaves differently with different types of objects, as it attempts to produce the most relevant, 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 attributes 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__', '__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']>>> class Shape(object):        def __dir__(self):            return ['area', 'perimeter', 'location']>>> s = Shape()>>> dir(s)

['Region', 'perimeter ', 'location']

Note Because dir () is supplied primarily as a convenience for use at an interactive prompt, it tries to supply an interesting set of names more than it tries to supply a rigorously or consistently defined set of names, and its detailed behavior may change your SS releases. for example, metaclass attributes are not in the result list when the argument is a class.


Note: no special instructions are provided for modifying all code instances in the series of articles, which are based on 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']

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.