I've got this code:
class Chars:
char_to_number = {
'a': ['1'],
'b': ['2'],
'c': ['3', '6'],
'd': ['4', '5'],
}
number_to_char = {
number: char
for char in char_to_number
for number in char_to_number[char]
}
Now this code returns an error as such:
name 'char_to_number' is not defined
Now it looks like python could not parse the nested dictionary comprehension. It looks like the inner scope was not updated with the outer scope, which defined the char_to_number
variable.
I've solved this code with this implementation:
class Chars:
char_to_number = {
'a': ['1'],
'b': ['2'],
'c': ['3', '6'],
'd': ['4', '5'],
}
number_to_char = {
number: char
for char, optional_numbers in char_to_number.items()
for number in optional_numbers
}
Here I'm not using the char_to_number variable in the inner loop, and python succeeded to parse this code.
Of course, all of that happens in the global scope of the class. In the global python scope, it doesn't happen:
char_to_number = {
'a': ['1'],
'b': ['2'],
'c': ['3', '6'],
'd': ['4', '5'],
}
number_to_char = {
number: char
for char in char_to_number
for number in char_to_number[char]
}
Does anyone have any clue about that?