Can Python's map function call object member functions?

后端 未结 5 1823
無奈伤痛
無奈伤痛 2021-02-01 14:31

I need to do something that is functionally equivalent to this:

for foo in foos:
    bar = foo.get_bar()
    # Do something with bar

My first i

5条回答
  •  無奈伤痛
    2021-02-01 14:41

    You can use lambda

    for bar in map(lambda foo: foo.get_bar(), foos):
        # Do something with bar
    

提交回复
热议问题