1. 程式人生 > >[Python] The get() method on Python dicts and its "default" arg

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

ict argument ilb cnblogs user div ber class _for

# 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")

>>> greeting(382)
"Hi Alice!"

>>> greeting(333333)
"Hi there!"

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