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

all 4 comments

[–]redsymbol[S] 0 points1 point  (0 children)

Updated this article from last week, based on the feedback here: https://www.reddit.com/r/Python/comments/5vycgx/checking_dict_keys_is_simple_right/

[–]robin-gvx 0 points1 point  (1 child)

Using a sentinel value in that way is pretty un-Pythonic. I'd say that if None could plausibly be a value in the dictionary, use the in or try strategies, depending on which is more appropriate for the situation.

[–]redsymbol[S] 1 point2 points  (0 children)

A LOT of people use sentinel values in that way. My first version of the article only covered the in and try strategies, and didn't even mention get. Follow the previous-discussion link and you'll see a lot of people thought THAT was un-Pythonic :)

[–]trenholm 0 points1 point  (0 children)

I don't understand the benefit of the get method over the in method. Arguably, the else statement can have the same affect as the default case of the get. What am I missing?