This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Lambda assigned to object does not automatically get self
Type: behavior Stage:
Components: Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: James.Lu, amaury.forgeotdarc, brett.cannon, eric.smith
Priority: normal Keywords:

Created on 2013-07-16 14:20 by James.Lu, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (7)
msg193166 - (view) Author: James Lu (James.Lu) * Date: 2013-07-16 14:20
if you assign a lambda to a object and call it,you get this:
Traceback (most recent call last):
  File "<pyshell#21>", line 1, in <module>
    n.__div__(3)
TypeError: <lambda>() takes exactly 2 arguments (1 given)
The full test is here:
>>> n = num()
>>> n.__div__
<function <lambda> at 0x040B2DF0>
>>> n/3
Traceback (most recent call last):
  File "<pyshell#20>", line 1, in <module>
    n/3
TypeError: unsupported operand type(s) for /: 'num' and 'int'
>>> n.__div__(3)
Traceback (most recent call last):
  File "<pyshell#21>", line 1, in <module>
    n.__div__(3)
TypeError: <lambda>() takes exactly 2 arguments (1 given)
msg193167 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2013-07-16 14:23
What version of Python is this and did you assign the lambda to an instance or class (and if this is Python 2, new-style or classic class)?
msg193169 - (view) Author: James Lu (James.Lu) * Date: 2013-07-16 14:24
2.5,new-style
msg193170 - (view) Author: James Lu (James.Lu) * Date: 2013-07-16 14:25
instance,assinged during __init__
msg193171 - (view) Author: James Lu (James.Lu) * Date: 2013-07-16 14:36
Also,there were some bugs, but after I fixed them, it would only work if I did this:
n.__div__(n,3)
msg193173 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2013-07-16 14:48
Could you provide an entire example, showing the class num and how you assign __div__?
msg193175 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2013-07-16 15:00
This is expected. When you assign to "n.__div__" a function which takes two parameters, you have to call it with two parameters:

    aFunction = lambda x, y: (x, y)
    n.__div__ = aFunction
    aFunction(1, 2)
    n.__div__(1, 2)

After all, aFunction and n.__div__ are the same object.
Now, it would be different if you had attached the function to the *class* instead:

    n.__class__.__div__ = aFunction
    n.__div__(2)   # returns (n, 2)

In this second example, n.__div__ is a bound method; the first parameter (usually named "self") is already filled, and only the second one is required.
History
Date User Action Args
2022-04-11 14:57:48adminsetgithub: 62674
2013-07-16 15:00:17amaury.forgeotdarcsetstatus: open -> closed

nosy: + amaury.forgeotdarc
messages: + msg193175

resolution: not a bug
2013-07-16 14:48:19eric.smithsetnosy: + eric.smith
messages: + msg193173
2013-07-16 14:36:17James.Lusetmessages: + msg193171
2013-07-16 14:25:41James.Lusetmessages: + msg193170
2013-07-16 14:24:45James.Lusetmessages: + msg193169
2013-07-16 14:23:24brett.cannonsetnosy: + brett.cannon
messages: + msg193167
2013-07-16 14:21:18James.Lusettype: behavior
2013-07-16 14:20:54James.Lucreate