How dictionaries behave

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How dictionaries behave



Here's the code:


checkDictionary =
'0': (print('0'),0),
'1': (print('1'),1),
'2': (print('2'),2)



print(checkDictionary.get('1','Not found'))



The output I expected was:


1
(None, 1)



but instead it runs all the functions in every value field:


0
1
2
(None, 1)



Can anyone explain this behavior, and why was it implemented like this?



I heard from multiple sources that Python dictionaries are supposed to be O(1) fast "most of the time".



So the thing I really wanted to accomplish was to make a nice switch/case with multiple executing lines:


def readPoint(v):
print(f'Point: v')

def readLine(v):
print(f'Line: v')

Point = 1
Line = 2
segment = [1,1]
checkDictionary =
Point: "geometry = readPoint(segment)n__type='Point'",
Line: "geometry = readLine(segment)n__type='Line'"



exec(checkDictionary.get(segment[0],'raise ValueError("Some error")'))



Would there be some issues with this design pattern? don't care about the functions or variables they are only examples.





The print statements execute when checkDictionary is declared and the dictionary is left full of literals i.e. '0': (None, 0), '1': (None, 1), '2': (None, 2), '3': (None, 3), '4': (None, 4), '5': (None, 5), '6': (None, 6), '7': (None, 7).
– Bill
Aug 8 at 7:20



checkDictionary


'0': (None, 0), '1': (None, 1), '2': (None, 2), '3': (None, 3), '4': (None, 4), '5': (None, 5), '6': (None, 6), '7': (None, 7)





It's not clear to me how any of the rest of your question relates to the last sentence. This isn't at all specific to dictionaries, a list [(print('0'), 0)] would also print when defined then contain (None, 0).
– jonrsharpe
Aug 8 at 7:29



[(print('0'), 0)]


(None, 0)





The print function was put in there as example. I tried at first to put som exec function to run multiple lines of code, but was struct seeing that all my exec lines were run nevertheless if it coinsided with my key value.
– Paal Pedersen
Aug 8 at 8:00




2 Answers
2



This is totally expected and in no way a strange behavior. The key to understanding is knowing that the dictionary gets created (keys and values are evaluated) when you declare it.



In your case, the evaluation of the values results in the execution of the print statements.


print



This is why you get:


0
1
2
3
4
5
6
7



Then comes print(checkDictionary.get('1','Not found'))


print(checkDictionary.get('1','Not found'))



which expectedly produces:


(None, 1)





Thanks, so instead if I want a function to go in as a value I can make it a string and evaluate the result.
– Paal Pedersen
Aug 8 at 7:27





you could but evaluating strings can be a pandora's box.
– Ev. Kounis
Aug 8 at 7:29






@PaalPedersen functions are first class in Python, you can put them in as values if you don't call them. Look into lambda or functools.partial for creating callables with preset arguments. I wouldn't recommend stringifying and evaluating.
– jonrsharpe
Aug 8 at 7:31



lambda


functools.partial



It does it because anywhere you're calling print it will print, so the best way to solve this is to remove the print or something else and also print is a none type


print


print



Actually (None, 1) is the output of the code, it's just that when you're creating the dictionary it is printing


(None, 1)



so at the end your code is working as expected






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard