Is there a map without result in python?

2019-01-15 05:44发布

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

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

Sometimes I need this stuff for module initialization, so I don't want to have a footprint like x in global namespace. One solution would be to just use map together with lambda:

map(lambda x: installWow(x, 'installed by me'), wowList)

But this of course creates a nice list [None, None, ...] so my question is, if there is a similar function without a return-list -- since I just don't need it.

(off course I can also use _x and thus not leaving visible footprint -- but the map-solution looks so neat ...)

14条回答
beautiful°
2楼-- · 2019-01-15 06:13

You might try this:

filter(lambda x: installWow(x, 'installed by me') and False, wowList)

That way, the return result is an empty list no matter what.

Or you could just drop the and False if you can force installWow() to always return False (or 0 or None or another expression that evaluates false).

查看更多
干净又极端
3楼-- · 2019-01-15 06:13

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']
查看更多
聊天终结者
4楼-- · 2019-01-15 06:17

How about this?

for x in wowList:
    installWow(x, 'installed by me')
del x
查看更多
相关推荐>>
5楼-- · 2019-01-15 06:17

Every expression evaluates to something, so you always get a result, whichever way you do it. And any such returned object (just like your list) will get thrown away afterwards because there's no reference to it anymore.

To clarify: Very few things in python are statements that don't return anything. Even a function call like

doSomething()

still returns a value, even if it gets discarded right away. There is no such thing as Pascal's function / procedure distinction in python.

查看更多
仙女界的扛把子
6楼-- · 2019-01-15 06:21

You could make your own "each" function:


def each(fn, items):
    for item in items:
        fn(item)


# called thus
each(lambda x: installWow(x, 'installed by me'), wowList)

Basically it's just map, but without the results being returned. By using a function you'll ensure that the "item" variable doesn't leak into the current scope.

查看更多
祖国的老花朵
7楼-- · 2019-01-15 06:31

first rewrite the for loop as a generator expression, which does not allocate any memory.

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

But this expression doesn't actually do anything without finding some way to consume it. So we can rewrite this to yield something determinate, rather than rely on the possibly None result of installWow.

( [1, installWow(x,  'installed by me')][0] for x in wowList )

which creates a list, but returns only the constant 1. this can be consumed conveniently with reduce

reduce(sum, ( [1, installWow(x,  'installed by me')][0] for x in wowList ))

Which conveniently returns the number of items in wowList that were affected.

查看更多
登录 后发表回答