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 ()