码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-10-01 19:02:43      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:get   tin   col   def   user   class   python   default   argument   

# 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

标签:get   tin   col   def   user   class   python   default   argument   

原文地址:http://www.cnblogs.com/Answer1215/p/7617569.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!