Python dict.get('key') versus dict['key'] [duplicate]
Asked Answered
Q

4

31

Why does this throw a KeyError:

d = dict()
d['xyz']

But this does not?

d = dict()
d.get('xyz')

I'm also curious if descriptors play a role here.

Qualification answered 21/5, 2015 at 2:28 Comment(3)
That's basically the entire practical difference between the two ways - it's there so that you can handle None (or a specified default) or exceptions at your discretion. Are you curious about the implementation details?Carpophore
Thanks, yes, I am curious about the lower-level details, specifically how a KeyError is avoided with the 'if k in d' construct. And is it a KeyError with d['xyz'] (if 'xyz' doesn't exist) because this is 'raw' or direct access to a dict object? It seems like the answer to my question as i've stated it is 'because this is how it works', so maybe I'm making too much of what are ultimately design decision, i.e. when to raise a fuss and when not to.Qualification
The lower level details aren't hard to imagine; you could write the equivalent method yourself by just returning the second argument passed in on keyError.Pylon
C
31

This is simply how the get() method is defined.

From the Python docs:

Return the value for key if key is in the dictionary, else default. If default is not given, it defaults to None, so that this method never raises a KeyError.

The default "not-found" return value is None. You can return any other default value.

d = dict()
d.get('xyz', 42)  # returns 42
Cadmar answered 21/5, 2015 at 2:30 Comment(0)
D
5

Accessing by brackets does not have a default but the get method does and the default is None. From the docs for get (via a = dict(); help(a.get))

Help on built-in function get:

get(...)
    D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None.
Dermott answered 21/5, 2015 at 2:31 Comment(0)
B
3

Simply because [ 1 ] the key is not in the map and [ 2 ] those two operations are different in nature.

From dict Mapping Types:

d[key]

Return the item of d with key key. Raises a KeyError if key is not in the map.

get(key[, default])

Return the value for key if key is in the dictionary, else default. If default is not given, it defaults to None, so that this method never raises a KeyError.

Bigwig answered 21/5, 2015 at 3:7 Comment(0)
M
1

Your opening question is well answered, I believe, but I don't see any response to

I'm also curious if descriptors play a role here.

Technically, descriptors do play a role here, since all methods are implemented implicitly with a descriptor, but there are no clear explicit descriptors being used, and they have nothing to do with the behavior you're questioning.

Marcomarconi answered 16/7, 2015 at 14:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.