Python built-in function 3-dir ()

Source: Internet
Author: User
Tags list of attributes unpack

Help on built-in function dir in module __builtin__:


Dir (...)

Dir ([object]), List of strings

If called without an argument, return the names of the current scope.

Else, return an alphabetized list of names comprising (some of) the attributes

Of the given object, and of attributes reachable from it.

If the object supplies a method named __dir__, it'll be used; otherwise

The default Dir () logic is used and returns:

For a module object:the module ' s attributes.

For a class object:its attributes, and recursively the attributes

of its bases.

For the object:its attributes, its class ' s attributes, and

Recursively the attributes of its class ' s base classes.


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 would be called and must return the list of attributes. This allows objects, implement a custom __getattr__ () or __getattribute__ () function to customize the by Dir () report s their attributes.


If The object does not provide __dir__ (), the function tries its best to gather information from the object ' s __dict__ att Ribute, if defined, and from its type object. The resulting list is not necessarily complete, and could be inaccurate if the object has a custom __getattr__ ().


The default dir () mechanism behaves differently with different types of objects, as it attempts-produce the most Releva NT, 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 A Ttributes 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)

[' Area ', ' perimeter ', ' location ']


Note Because dir () is supplied primarily as a convenience for use in an interactive prompt, it tries-supply an interest ing set of names more than it tries to supply a rigorously or consistently defined set of names, and its detailed behavior may change across releases. For example, Metaclass attributes was not in the result list, and the argument is a class.


英文说明: Returns a list of the types of variables, methods, and definitions in the current scope without parameters, and returns a list of properties and methods for parameters when they are with parameters. If the parameter contains method __dir__ (), the method is called. If the parameter does not contain __dir__ (), the method will collect the parameter information to the fullest extent.


Parameter: Object, variable, type.


>>> dir ()

[' Person ', ' __builtins__ ', ' __doc__ ', ' __name__ ', ' __package__ ', ' a ', ' cmpcode ', ' Code ', ' I ', ' str ', ' Tom ']

>>> Import struct

>>> dir ()

[' Person ', ' __builtins__ ', ' __doc__ ', ' __name__ ', ' __package__ ', ' a ', ' cmpcode ', ' Code ', ' I ', ' str ', ' struct ', ' Tom ']

>>> 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 ']


This article is from the "Big Cloud Technology" blog, please be sure to keep this source http://hdlptz.blog.51cto.com/12553181/1899555

Python built-in function 3-dir ()

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.