0
a = types.SimpleNamespace()
b = dict()
[a.__setattr__(key, value) for key, value in b]

Now I would like to use map: list(map(a.setattr, b.items())), but the item of b.items() is a tuple, not key and value. I know the Python has the unpack syntax, so I am curious is there any function can unpack sequence too so that I can use it in map.

acgtyrant
  • 1,721
  • 1
  • 16
  • 24

2 Answers2

0

You can use itertools.starmap:

In [46]: list(itertools.starmap(a.__setattr__, {'a': '1', 'b': '2'}.items()))
Out[46]: [None, None]

In [47]: a
Out[47]: namespace(a='1', b='2')

Though I do not think that list comprenehsion or list(map is more readable than a simple loop here.

awesoon
  • 32,469
  • 11
  • 74
  • 99
0

You can use itertools.starmap to map the sequence of key-value pairs to a.setattr:

list(starmap(a.__setattr__, b.items()))
blhsing
  • 91,368
  • 6
  • 71
  • 106