Learning notes ——— python built-in Functions dir ()

Source: Internet
Author: User

dir([object])----can take parameters or without parameters

1. Returns a list of the types of variables, methods, and definitions within the current scope when no arguments are taken.

>>>dir ()

[' __annotations__ ', ' __builtins__ ', ' __doc__ ', ' __loader__ ', ' __name__ ', ' __pack '

age__ ', ' __spec__ ', ' OS '] # with import OS

>>> l =[] #创建名为 l list (switch to Notepad directory)

>>> dir ()
[' __annotations__ ', ' __builtins__ ', ' __doc__ ', ' __loader__ ', ' __name__ ', ' __pack '
age__ ', ' __spec__ ', ' l ']

>>> Import OS #多了import os
>>> dir ()
[' __annotations__ ', ' __builtins__ ', ' __doc__ ', ' __loader__ ', ' __name__ ', ' __pack '
age__ ', ' __spec__ ', ' l ', ' OS '

2. When the Parameter object is a module, returns a list of properties and methods for the module.

>>> Import Math
>>> dir (Math)
[' __doc__ ', ' __loader__ ', ' __name__ ', ' __package__ ', ' __spec__ ', ' ACOs ', ' Acosh '
, ' ASIN ', ' Asinh ', ' atan ', ' atan2 ', ' Atanh ', ' ceil ', ' copysign ', ' cos ', ' cosh ',
' Degrees ', ' e ', ' Erf ', ' erfc ', ' exp ', ' expm1 ', ' fabs ', ' factorial ', ' floor ', ' FM
Od ', ' frexp ', ' fsum ', ' gamma ', ' gcd ', ' hypot ', ' inf ', ' isclose ', ' isfinite ', ' is
Inf ', ' isNaN ', ' ldexp ', ' lgamma ', ' Log ', ' log10 ', ' log1p ', ' log2 ', ' modf ', ' nan '
, ' pi ', ' pow ', ' radians ', ' sin ', ' sinh ', ' sqrt ', ' tan ', ' tanh ', ' tau ', ' trunc ']

3. When the parameter object is a class, returns a list of properties and methods for the class and its parent class.

>>> class Student ():
... pass
...
>>> dir (Student)
[' __class__ ', ' __delattr__ ', ' __dict__ ', ' __dir__ ', ' __doc__ ', ' __eq__ ', ' __form '
At__ ', ' __ge__ ', ' __getattribute__ ', ' __gt__ ', ' __hash__ ', ' __init__ ', ' __init_s '
Ubclass__ ', ' __le__ ', ' __lt__ ', ' __module__ ', ' __ne__ ', ' __new__ ', ' __reduce__ ',
' __reduce_ex__ ', ' __repr__ ', ' __setattr__ ', ' __sizeof__ ', ' __str__ ', ' __subclas
Shook__ ', ' __weakref__ ']

>>> class Student ():
... def count (self):
... return 100
...
>>> dir (Student)
[' __class__ ', ' __delattr__ ', ' __dict__ ', ' __dir__ ', ' __doc__ ', ' __eq__ ', ' __form '
At__ ', ' __ge__ ', ' __getattribute__ ', ' __gt__ ', ' __hash__ ', ' __init__ ', ' __init_s '
Ubclass__ ', ' __le__ ', ' __lt__ ', ' __module__ ', ' __ne__ ', ' __new__ ', ' __reduce__ ',
' __reduce_ex__ ', ' __repr__ ', ' __setattr__ ', ' __sizeof__ ', ' __str__ ', ' __subclas
Shook__ ', ' __weakref__ ', ' count '] #多一个count方法

4. When an object inherits a class that defines the__dir__方法,则dir(对象)返回__dir__方法的结果,而dir(父类)则返回类的方法、属性列表。

>>> class D ():
... def __dir__ (self):
... return [to] #即使不return一个list, and finally Dir (object) is still displayed as a list
...
>>> m = D ()
>>> dir (m)
[1, 2]

Learning notes ——— python built-in Functions dir ()

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.