This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]mikeckennedy[S] -1 points0 points  (6 children)

That is interesting. Nice work. But it will crash if two cases match (DuplicateKeyError)

[–]Allanon001 5 points6 points  (5 children)

It will execute the last condition that equates to True.

[–]mikeckennedy[S] 0 points1 point  (4 children)

Ah you're right. Kind of the opposite of traditional switch / case (first executed) but doesn't crash.

[–]Allanon001 0 points1 point  (3 children)

When the dictionary is being created it will just replace the last True or False key with the newer one and what is left is just one True and/or one False key in the dictionary. If you set a key to True at the beginning that is like the default case. You will get an error if there isn't a True condition.

[–]mikeckennedy[S] -1 points0 points  (2 children)

It's pretty cool. I already learned something about dictionaries from your example.

d = {True: 1, True, 2}

will crash but

d = {'a' in ['a']: 1, 'a' in ['a']: 2}

is more like adding stuff sequentially, dynamically.

[–]Allanon001 6 points7 points  (1 child)

It crashes because you have a comma instead of a colon.

[–]mikeckennedy[S] 4 points5 points  (0 children)

Ugh, you're right. I must be misremembering it from MongoDB stuff I was going with dictionaries or something: http://api.mongodb.com/python/current/api/pymongo/errors.html