The functional map(func,iterable)
could be easily applied to normal functions, but if I want to apply the a_method
to a list of instances, how can I do it? I know list comprehensions can do this in a snap, but still want to see if a functional way exist and also feels pythonic.
class cls(object):
def __init__(self,val):
self.val=val
def clstest(self,val):
return self.val==val
a=cls(9)
b=cls(18)
c=cls(19)
lst=[a,b,c]
Then the following works:list(map(repr,lst))
returns
['<__main__.Cls object at 0x00000000070EDB70>',
'<__main__.Cls object at 0x00000000070EDBE0>',
'<__main__.Cls object at 0x00000000070EDE80>']
, but list(map(clstest(10),lst))
will have error msg:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-165-f10396f98b58> in <module>()
----> 1 list(map(clstest,lst))
NameError: name 'clstest' is not defined
Update: correct my mistake in defining the class. The error msg still remains.