can anyone explain to me the why the design choice was made, that when calling __iter__
on a Python dictionary this should iterate over the key values only?
I might be biased by coming from C++, where - when iterating over a std::map
- one gets std::map::value_type &
[i.e. std::pair &
] values.
I encountered this question when debugging strange program behavior, where I had a situtation similar to the following one:
def method(one=None, two=None, three=None):
print('one: {one}, two: {two}, three: {three}'.format(one=one, two=two, three=three))
return
if __name__ == '__main__':
blub = {'one': 1, 'two': 2, 'three': 3}
method(*blub)
exit(0)
with the following output
one: one, two: two, three: three
whereas I would have expected
one: 1, two: 2, three: 3
. My mistake was not to call method(**blub)
obviously, but - again, I come from C++ - it took me some time to find this out.
Additionally I would have liked a warning, that passing a dict
with one asterisk only may be undesired - but that's not how Python works, is it?
Might I have detected my error more easily using e.g. method(**kwargs)
? I don't see how, but maybe someone can enlighten me here as well.
Thanks in advance
Johannes