user1691278
user1691278

Reputation: 1885

What does this "none" do in .get() in Python?

I'm trying to learn Python through Learn Python the Hard Way. I'm now on Exercise 39 and have a simple question. I tried to search online but couldn't find any answers. Here's the code from the exercise:

# create a mapping of state to abbreviation
states = {
    'Oregon': 'OR',
    'Florida': 'FL',
    'California': 'CA',
    'New York': 'NY',
    'Michigan': 'MI'
}

# create a basic set of states and some cities in them
cities = {
    'CA': 'San Francisco',
    'MI': 'Detroit',
    'FL': 'Jacksonville'
}

print '-' * 10
# safely get a abbreviation by state that might not be there
state = states.get('Texas', None)

if not state:
    print "Sorry, no Texas."

# get a city with a default value
city = cities.get('TX', 'Does Not Exist')
print "The city for the state 'TX' is: %s" % city

I don't quite understand what None does in state = states.get('Texas', None). If it were used to tell Python that "there is no more," then why couldn't I just write state = states.get('Texas')? What do I need the extra None here? Thanks!

Upvotes: 2

Views: 2693

Answers (3)

Smart Manoj
Smart Manoj

Reputation: 5824

Passing None takes extra memory for the instruction LOAD_CONST.

Python 3.11.0a7 (main, Apr  5 2022, 21:27:39) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import dis
>>> a = {}
>>> dis.dis("a.get(2)")
  1           0 LOAD_NAME                0 (a)
              2 LOAD_METHOD              1 (get)
              4 LOAD_CONST               0 (2)
              6 CALL_METHOD              1
              8 RETURN_VALUE
>>> dis.dis("a.get(2, None)")
  1           0 LOAD_NAME                0 (a)
              2 LOAD_METHOD              1 (get)
              4 LOAD_CONST               0 (2)
              6 LOAD_CONST               1 (None)
              8 CALL_METHOD              2
             10 RETURN_VALUE

Upvotes: 0

Gareth Latty
Gareth Latty

Reputation: 89017

The None here is a default value for dict.get() telling python what to return if the key does not exist. It is a little superfluous in this case, as get() defaults to None for the second parameter.

From the docs:

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.

So it directly equivalent to states.get('Texas') which is equivalent to:

try:
    state = states['Texas']
except KeyError:
    state = None

So in this case, where you are then simply checking if state doesn't evaluate to True, it's pointless, it'd be better to simply do the try/except and perform the action you want on the exception (unless for some reason you are getting back other values you are also filtering out, such as empty strings, which seems unlikely).

The Python docs are detailed but clear, so in situations where you don't know what an argument is, check the docs.

Upvotes: 6

squiguy
squiguy

Reputation: 33370

You don't need to write the None, that is the default return value if the key was not found in the dictionary. It is superfluous in this case.

You can replace it to return something else like Not found for example.

Upvotes: 2

Related Questions