Easy extraction of python import package properties method
- 2020-12-18 01:52:27
- OfStack
dir is the most convenient, but if you want to know a specific attribute that starts with _, you need to write a filter. Here are two ways to implement the filter
# method 1
def e(start='_', module='os'):
module = __import__(module)
def gen_attr():
for attr in dir(module):
if attr.startswith(start):
yield attr
yield from gen_attr()
# method 2 The generator derivation is more concise
def e2(start='', module='os'):
module = __import__(module)
yield from (attr for attr in dir(module) if attr.startswith(start))
if __name__ == '__main__':
print (list(e('')))
print (list(e2('a')))