Skip to content

report very strange behavior of user-defined class lambda function #126666

@gseismic

Description

@gseismic

Bug report

Bug description:

class X:
    # HARD to define a pure lambda function in class
    fn_a = lambda x: x

    def test(self):
        print(f'{type(self.fn_a)=}')  # type(self.fn_a)=<class 'method'>
        assert self.fn_a() == self    # OK
        # assert self.fn_a(1) == 1    # ERROR: TypeError: X.<lambda>() takes 1 positional argument but 2 were given

    @classmethod
    def cls_test(cls):
        print(f'{type(cls.fn_a)=}') # type(cls.fn_a)=<class 'function'>
        assert cls.fn_a(1) == 1     # OK


x = X()
x.cls_test()
x.test()

print(f'{X.fn_a=}') # X.fn_a=<function X.<lambda> at 0x103086b60>

It is very HARD to define a pure lambda function in class, the result is unexpected.
I’m not sure if it was intentionally designed this way or if I missed something.

CPython versions tested on:

3.12

Operating systems tested on:

macOS

### Tasks

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions