Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't add a method to None #8

Open
zed opened this issue Mar 10, 2014 · 4 comments
Open

Can't add a method to None #8

zed opened this issue Mar 10, 2014 · 4 comments

Comments

@zed
Copy link

zed commented Mar 10, 2014

$ python
Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from forbiddenfruit import curse
>>> None.method()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'method'
>>> curse(type(None), 'method', lambda *a, **kw: None)
>>> None.method()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unbound method <lambda>() must be called with NoneType instance as first argument (got nothing instead)
@candeira
Copy link

candeira commented Aug 6, 2014

Hi, zed. Since None is a global singleton, it's possible that you can live with a static method:

>>> curse(type(None), 'method', staticmethod(lambda *a, **kw: 'foo'))
>>> None.method()
'foo'

If you want to patch it reusing a generic method where self represents the bound instance, you can cheat this way:

>>> from functools import partial
>>> curse(type(None), 'method', partial((lambda self, *a, **kw: repr(self)), None))
>>> None.method()
'None'

@clarete
Copy link
Owner

clarete commented Aug 6, 2014

@candeira Awesome input! I loved the last snippet, pretty awesome usage of the partial() utility!

@candeira
Copy link

candeira commented Aug 7, 2014

@clarete thanks!

Also, you ain't seen nothing yet! :)

@clarete
Copy link
Owner

clarete commented Aug 7, 2014

@candeira Awesome to have skilled people playing with the library! The project is pretty open to contributions! ✌️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants