Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why can't builtins become bound methods?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)?

>Solution :

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())
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading