mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 23:10:17 +00:00
explain empty sets
This commit is contained in:
@@ -478,7 +478,7 @@ IndexError: pop from empty list</samp></pre>
|
||||
<li>The original list is unchanged.
|
||||
</ol>
|
||||
|
||||
<p>Don’t have any values yet? Not a problem. You can create an empty set.
|
||||
<p id=emptyset>Don’t have any values yet? Not a problem. You can create an empty set.
|
||||
|
||||
<pre class=screen>
|
||||
<a><samp class=p>>>> </samp><kbd class=pp>a_set = set()</kbd> <span class=u>①</span></a>
|
||||
|
||||
@@ -1260,7 +1260,7 @@ except:
|
||||
|
||||
<h3 id=set_literal><code>set()</code> literals (explicit)</h3>
|
||||
|
||||
<p>In Python 2, the only way to define a literal set in your code was to call <code>set(a_sequence)</code>. This still works in Python 3, but a clearer way of doing it is to use the new set literal notation: curly braces. (Dictionaries are also defined with curly braces, which makes sense once you think about it, because dictionaries are just sets of key-value pairs.)
|
||||
<p>In Python 2, the only way to define a literal set in your code was to call <code>set(a_sequence)</code>. This still works in Python 3, but a clearer way of doing it is to use the new set literal notation: curly braces. This works for everything except empty sets, because dictionaries also use curly braces, so <a href=native-datatypes.html#emptyset><code>{}</code> is an empty dictionary, not an empty set</a>.
|
||||
|
||||
<blockquote class=note>
|
||||
<p><span class=u>☞</span>The <code>2to3</code> script will not fix <code>set()</code> literals by default. To enable this fix, specify <kbd>-f set_literal</kbd> on the command line when you call <code>2to3</code>.
|
||||
|
||||
Reference in New Issue
Block a user