我正在学习python,我有一个关于初始化集的新手问题。 通过测试,我发现一个集合可以像这样初始化:
my_set = {'foo', 'bar', 'baz'}这样做有什么不利之处,而不是标准的方式:
my_set = set(['foo', 'bar', 'baz'])还是只是一个风格的问题?
I'm learning python, and I have a novice question about initializing sets. Through testing, I've discovered that a set can be initialized like so:
my_set = {'foo', 'bar', 'baz'}Are there any disadvantages of doing it this way, as opposed to the standard way of:
my_set = set(['foo', 'bar', 'baz'])or is it just a question of style?
最满意答案
set literal语法有两个明显的问题:
my_set = {'foo', 'bar', 'baz'}Python 2.7之前不可用
没有办法使用该语法来表达一个空集(使用{}创建一个空的dict)
那些对你来说可能或可能不重要。
文档的这一部分概述了这种语法。
There are two obvious issues with the set literal syntax:
my_set = {'foo', 'bar', 'baz'}It's not available before Python 2.7
There's no way to express an empty set using that syntax (using {} creates an empty dict)
Those may or may not be important to you.
The section of the docs outlining this syntax is here.
更多推荐
发布评论