mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 23:10:17 +00:00
added clarification about lambda functions that take a tuple instead of multiple parameters [h/t G.B.]
This commit is contained in:
@@ -793,7 +793,7 @@ except:
|
||||
<li>If you used to call <code>xreadlines()</code> with an argument (the number of lines to read at a time), keep doing that. It still works in Python 3, and <code>2to3</code> will not change it.
|
||||
</ol>
|
||||
<p class=c><span style="font-size:56px;line-height:0.88">☃</span>
|
||||
<h2 id=tuple_params><code>lambda</code> functions with multiple parameters</h2>
|
||||
<h2 id=tuple_params><code>lambda</code> functions that take a tuple instead of multiple parameters</h2>
|
||||
<p>In Python 2, you could define anonymous <code>lambda</code> functions which took multiple parameters by defining the function as taking a tuple with a specific number of items. In effect, Python 2 would “unpack” the tuple into named arguments, which you could then reference (by name) within the <code>lambda</code> function. In Python 3, you can still pass a tuple to a <code>lambda</code> function, but the Python interpreter will not unpack the tuple into named arguments. Instead, you will need to reference each argument by its positional index.
|
||||
<table>
|
||||
<tr><th>Notes</th>
|
||||
@@ -809,11 +809,15 @@ except:
|
||||
<tr><th>③</th>
|
||||
<td><code>lambda (x, (y, z)): x + y + z</code></td>
|
||||
<td><code>lambda x_y_z: x_y_z[0] + x_y_z[1][0] + x_y_z[1][1]</code></td></tr>
|
||||
<tr><th>④</th>
|
||||
<td><code>lambda x, y, z: x + y + z</code></td>
|
||||
<td><i>unchanged</i></td></tr>
|
||||
</table>
|
||||
<ol>
|
||||
<li>If you had defined a <code>lambda</code> function that took a tuple of one item, in Python 3 that would become a <code>lambda</code> with references to <var>x1[0]</var>. The name <var>x1</var> is autogenerated by the <code>2to3</code> script, based on the named arguments in the original tuple.
|
||||
<li>A <code>lambda</code> function with a two-item tuple <var>(x, y)</var> gets converted to <var>x_y</var> with positional arguments <var>x_y[0]</var> and <var>x_y[1]</var>.
|
||||
<li>The <code>2to3</code> script can even handle <code>lambda</code> functions with nested tuples of named arguments. The resulting Python 3 code is a bit unreadable, but it works the same as the old code did in Python 2.
|
||||
<li>You can define <code>lambda</code> functions that take multiple arguments. Without parentheses around the arguments, Python 2 just treats it as a <code>lambda</code> function with multiple arguments; within the <code>lambda</code> function, you simply reference the arguments by name, just like any other function. This syntax still works in Python 3.
|
||||
</ol>
|
||||
<h2 id=methodattrs>Special method attributes</h2>
|
||||
<p>In Python 2, class methods can reference the class object they are defined in, as well as the method object itself. <code>im_self</code> is the class instance object; the class <code>im_func</code> is the function object; <code>im_class</code> is the class of <code>im_self</code> (for bound methods) or the class that asked for the method (for unbound methods). In Python 3, these special method attributes have been renamed to follow the naming conventions of other attributes.
|
||||
|
||||
@@ -332,7 +332,7 @@ ul li ol{margin:0;padding:0 0 0 2.5em}
|
||||
<li><a href=porting-code-to-python-3-with-2to3.html#raw_input><code>raw_input()</code> and <code>input()</code> global functions</a>
|
||||
<li><a href=porting-code-to-python-3-with-2to3.html#funcattrs><code>func_*</code> function attributes</a>
|
||||
<li><a href=porting-code-to-python-3-with-2to3.html#xreadlines><code>xreadlines()</code> I/O method</a>
|
||||
<li><a href=porting-code-to-python-3-with-2to3.html#tuple_params><code>lambda</code> functions with multiple parameters</a>
|
||||
<li><a href=porting-code-to-python-3-with-2to3.html#tuple_params><code>lambda</code> functions that take a tuple instead of multiple parameters</a>
|
||||
<li><a href=porting-code-to-python-3-with-2to3.html#methodattrs>Special method attributes</a>
|
||||
<li><a href=porting-code-to-python-3-with-2to3.html#nonzero><code>__nonzero__</code> special class attribute</a>
|
||||
<li><a href=porting-code-to-python-3-with-2to3.html#numliterals>Octal literals</a>
|
||||
|
||||
Reference in New Issue
Block a user