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

python tips:省略括号

时间:2019-07-10 20:07:49      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:版本   tips   生成   情况   两种   函数   code   range   hello   

在Python中,有两种情况下可以省略括号。

  1. 将生成器作为函数的唯一参数
  2. 元组作为字典的键

示例如下

# 正常版本
s = sum((i for i in range(10)))
# 省略括号
s = sum(i for i in range(10))
# 正常版本
s = "".join((i for i in "hello world"))
# 省略括号
s = "".join(i for i in "hello world")
# 字典
s = {(1, 2, 3): "hello world"}
print(s[(1, 2, 3)], s[1, 2, 3])

输出结果

hello world hello world

python tips:省略括号

标签:版本   tips   生成   情况   两种   函数   code   range   hello   

原文地址:https://www.cnblogs.com/luoheng23/p/11166104.html

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