Is there a map without result in python?

前端 未结 15 1123
北恋
北恋 2020-12-03 04:33

Sometimes, I just want to execute a function for a list of entries -- eg.:

for x in wowList:
   installWow(x, \'installed by me\')

Sometime

15条回答
  •  悲&欢浪女
    2020-12-03 05:14

    Someone needs to answer --

    The more pythonic way here is to not worry about polluting the namespace, and using __all__ to define the public variables.

    myModule/__init__.py:
         __all__ = ['func1', 'func2']
    
         for x in range(10): 
             print 'init {}'.format(x)
    
         def privateHelper1(x):
             return '{}:{}'.format(x,x)
    
         def func1(): 
             print privateHelper1('func1')
    
         def func2(): 
             print privateHelper1('func1')
    

    Then

    python -c "import myModule; help(myModule);"
    
    init 0
    init 1
    init 2
    init 3
    init 4
    init 5
    init 6
    init 7
    init 8
    init 9
    Help on package mm:
    
    NAME
        myModule
    
    FILE
        h:\myModule\__init__.py
    
    PACKAGE CONTENTS
    
    
    FUNCTIONS
        func1()
    
       func2()
    
    DATA
       __all__ = ['func1', 'func2']
    

提交回复
热议问题