Problems with passing by reference in Python 2d
Take this dict dicts:
case_forms = {'plural': {'nominative': 'dni', 'locative': 'dniach'},
'singular': {'instrumental': 'dniem', 'vocative': 'dzie\xc5\x84'}}
I would like to get a list of all (a, b) key pairs that can be used like case_forms[a][b]
.
No problem, right? Accounting for a double list. Do things like this all the time in Haskell:
[(number, case_name) for case_name in case_dict.keys() for number, case_dict in case_forms.items()]
Also, it won't produce the expected result:
[('plural', 'instrumental'), ('singular', 'instrumental'), ('plural', 'vocative'), ('singular', 'vocative')]
I am wondering how to solve this problem. No amount of cleverly placed [:]
seems to do the trick.
source to share
How about this:
[ (number, case_name) for number, case_dict in case_forms.items() for case_name in case_dict.keys() ]
Edited to link to @ juanpa.arrivillaga. Comments on why my example was behaving strangely:
Python 2 concepts have fuzzy scope and it doesn't use case_dict as you think it uses case_dict from the previous understanding (which leaked out into the outer scope)
Start a new interpreter session and you will get a NameError
source to share