程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python built-in functions - dir()

編輯:Python

1、 The official definition of

Format :dir([object])

There is no argument , Returns the list of names in the current local scope .

With arguments , Returns a list of valid properties for a parameter object .

because python Everything is an object , all object Object can be a module , type , class , function , Method , Properties, etc .

2、dir() function

2.1 With no arguments

Returns the list of names in the current local scope

>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']
>>>

2.2 With parameters

Returns a list of valid properties for a parameter object

>>> import string
>>> dir(string)
['Formatter', 'Template', '_ChainMap', '_TemplateMetaclass', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_re', '_sentinel_dict', '_string', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'capwords', 'digits', 'hexdigits', 'octdigits', 'printable', 'punctuation', 'whitespace']
>>>

Be careful :

        dir() By default, a list of all attributes and methods of the object will be output , Contains double underlined “__” The beginning and the end , Private methods that cannot be called externally . For ease of reading , We need to not show these special members , Here are two ways .

(1) If the object has __all__ Variable , You can use it directly , As in the above example, you can use string.__all__

>>> import string
>>> dir(string)
['Formatter', 'Template', '_ChainMap', '_TemplateMetaclass', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_re', '_sentinel_dict', '_string', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'capwords', 'digits', 'hexdigits', 'octdigits', 'printable', 'punctuation', 'whitespace']
>>> string.__all__
['ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'capwords', 'digits', 'hexdigits', 'octdigits', 'printable', 'punctuation', 'whitespace', 'Formatter', 'Template']
>>>

(2) Use string method str.startswith(), Filter out output lists that contain double underscores “__” Private methods at the beginning and end .

import string


# Do not display special members of the object
lst = [i for i in dir(string) if not i.startswith('_')]

print(lst)

-----------------------------------------------------------------------------------
Running results :
['Formatter', 'Template', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'capwords', 'digits', 'hexdigits', 'octdigits', 'printable', 'punctuation', 'whitespace']

  • Either way ,__all__ Method sorts the output results ,dir() Function does not sort . But not all modules support the use of __all__ Variable , So for getting the members of some modules , You can only use dir() function .


refence:

Built in functions — Python 3.8.12 file


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved