diff --git a/porting-code-to-python-3-with-2to3.html b/porting-code-to-python-3-with-2to3.html index 727edaf..1bc265a 100644 --- a/porting-code-to-python-3-with-2to3.html +++ b/porting-code-to-python-3-with-2to3.html @@ -793,7 +793,7 @@ except:
  • If you used to call xreadlines() with an argument (the number of lines to read at a time), keep doing that. It still works in Python 3, and 2to3 will not change it.

    -

    lambda functions with multiple parameters

    +

    lambda functions that take a tuple instead of multiple parameters

    In Python 2, you could define anonymous lambda 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 lambda function. In Python 3, you can still pass a tuple to a lambda 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. @@ -809,11 +809,15 @@ except: + + +
    Notes
    lambda (x, (y, z)): x + y + z lambda x_y_z: x_y_z[0] + x_y_z[1][0] + x_y_z[1][1]
    lambda x, y, z: x + y + zunchanged

    1. If you had defined a lambda function that took a tuple of one item, in Python 3 that would become a lambda with references to x1[0]. The name x1 is autogenerated by the 2to3 script, based on the named arguments in the original tuple.
    2. A lambda function with a two-item tuple (x, y) gets converted to x_y with positional arguments x_y[0] and x_y[1].
    3. The 2to3 script can even handle lambda 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. +
    4. You can define lambda functions that take multiple arguments. Without parentheses around the arguments, Python 2 just treats it as a lambda function with multiple arguments; within the lambda function, you simply reference the arguments by name, just like any other function. This syntax still works in Python 3.

    Special method attributes

    In Python 2, class methods can reference the class object they are defined in, as well as the method object itself. im_self is the class instance object; the class im_func is the function object; im_class is the class of im_self (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. diff --git a/table-of-contents.html b/table-of-contents.html index 20d2325..c569516 100644 --- a/table-of-contents.html +++ b/table-of-contents.html @@ -332,7 +332,7 @@ ul li ol{margin:0;padding:0 0 0 2.5em}

  • raw_input() and input() global functions
  • func_* function attributes
  • xreadlines() I/O method -
  • lambda functions with multiple parameters +
  • lambda functions that take a tuple instead of multiple parameters
  • Special method attributes
  • __nonzero__ special class attribute
  • Octal literals