The get() method on Python dicts and its "default" arg

# The get() method on dicts
# and its "default" argument

name_for_userid = {
    382: "Alice",
    590: "Bob",
    951: "Dilbert",
}

def greeting(userid):
    return "Hi %s!" % name_for_userid.get(userid, "there")

# default를 "there"로 설정하여 userid를 잘못 입력하면 "there"가 반환된다.
--------------------------------------------------------------------------------
greeting(382)
'Hi Alice!'

greeting(3821)
'Hi there!'



댓글