我正在尝试通过 Learn Python the Hard Way 来学习 Python。我现在在练习 39 并且有一个简单的问题。我试图在网上搜索,但找不到任何答案。这是练习中的代码:
# 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
我不太明白None
in 的作用是什么state = states.get('Texas', None)
。如果它被用来告诉 Python “没有更多了”,那我为什么不能直接写state = states.get('Texas')
呢?我在None
这里需要什么额外的东西?谢谢!