Why can't builtins become bound methods?

Question:

class Something: pass
f=lambda : True

Now, if I do

Something.open=f
g=Something()
g.open()

I get an error that TypeError: <lambda>() takes 0 positional arguments but 1 was given and g.open is a <bound method <lambda> of <__main__.Something object at 0xffff80253400>>. This means that the self object as passed to open.

However, if I do

Something.open=open
g=Something()
g.open()

I just get an error that TypeError: open() missing required argument 'file' (pos 1), and g.open is just <built-in function open>, as presumably no arguments are given to open.

Why is there a difference?

Follow up: Can I get f to act like a built-in function (aka, get no self object passed to it)?

Asked By: DrownedSuccess

||

Answers:

I dont know why you would want to do this, but you can do:

class Something:
    pass
f=lambda : True
Something.open=staticmethod(f)
g=Something()
print(g.open())
Answered By: Tom McLean
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.