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)?
CodePudding user response:
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())