mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 15:00:18 +00:00
added #multivar section
This commit is contained in:
@@ -499,6 +499,40 @@ AttributeError: 'tuple' object has no attribute 'remove'</samp>
|
||||
<p><span class=u>☞</span>Tuples can be converted into lists, and vice-versa. The built-in <code>tuple()</code> function takes a list and returns a tuple with the same elements, and the <code>list()</code> function takes a tuple and returns a list. In effect, <code>tuple()</code> freezes a list, and <code>list()</code> thaws a tuple.
|
||||
</blockquote>
|
||||
|
||||
<h3 id=multivar>Assigning Multiple Values At Once</h>
|
||||
|
||||
<p>Here’s a cool programming shortcut: in Python, you can use a tuple to assign multiple values at once.
|
||||
|
||||
<pre class=screen>
|
||||
<samp class=p>>>> </samp><kbd class=pp>v = ('a', 2, True)</kbd>
|
||||
<a><samp class=p>>>> </samp><kbd>(x, y, z) = v</kbd> <span>①</span></a>
|
||||
<samp class=p>>>> </samp><kbd class=pp>x</kbd>
|
||||
<samp class=pp>'a'</samp>
|
||||
<samp class=p>>>> </samp><kbd class=pp>y</kbd>
|
||||
<samp class=pp>2</samp>
|
||||
<samp class=p>>>> </samp><kbd class=pp>z</kbd>
|
||||
<samp class=pp>True</samp></pre>
|
||||
<ol>
|
||||
<li><var>v</var> is a tuple of three elements, and <code>(x, y, z)</code> is a tuple of three variables. Assigning one to the other assigns each of the values of <var>v</var> to each of the variables, in order.
|
||||
</ol>
|
||||
|
||||
<p>This has all kinds of uses. I often want to assign names to a range of values. In <abbr>C</abbr>, you would use <code>enum</code> and manually list each constant and its associated value, which seems especially tedious when the values are consecutive. In Python, you can use the built-in <code>range()</code> function with multi-variable assignment to quickly assign consecutive values.
|
||||
|
||||
<pre class=screen>
|
||||
<a><samp class=p>>>> </samp><kbd class=pp>(MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY) = range(7)</kbd> <span class=u>②</span></a>
|
||||
<a><samp class=p>>>> </samp><kbd class=pp>MONDAY</kbd> <span class=u>③</span></a>
|
||||
<samp class=pp>0</samp>
|
||||
<samp class=p>>>> </samp><kbd class=pp>TUESDAY</kbd>
|
||||
<samp class=pp>1</samp>
|
||||
<samp class=p>>>> </samp><kbd class=pp>SUNDAY</kbd>
|
||||
<samp class=pp>6</samp></pre>
|
||||
<ol>
|
||||
<li>The built-in <code>range()</code> function constructs a sequence of integers. (Technically, the <code>range()</code> function returns an <a href=iterators.html>iterator</a>, not a list or a tuple, but you’ll learn about that distinction later.) <var>MONDAY</var>, <var>TUESDAY</var>, <var>WEDNESDAY</var>, <var>THURSDAY</var>, <var>FRIDAY</var>, <var>SATURDAY</var>, and <var>SUNDAY</var> are the variables you’re defining. (This example came from the <code>calendar</code> module, a fun little module that prints calendars, like the <abbr>UNIX</abbr> program <code>cal</code>. The <code>calendar</code> module defines integer constants for days of the week.)
|
||||
<li>Now each variable has its value: <var>MONDAY</var> is <code>0</code>, <var>TUESDAY</var> is <code>1</code>, and so forth.
|
||||
</ol>
|
||||
|
||||
<p>You can also use multi-variable assignment to build functions that return multiple values, simply by returning a tuple of all the values. The caller can treat it as a single tuple, or it can assign the values to individual variables. Many standard Python libraries do this, including the <code>os</code> module, which you'll learn about in <a href=comprehensions.html#os>the next chapter</a>.
|
||||
|
||||
<p class=a>⁂
|
||||
|
||||
<h2 id=sets>Sets</h2>
|
||||
|
||||
Reference in New Issue
Block a user