Patch python built-in objects
Maintained. Last release 5 days ago, commits in 2 of the last 26 weeks. Weaker on commit activity and release cadence.
pip install forbiddenfruitForbidden Fruit

This project allows Python code to extend built-in types.
If that's a good idea or not, you tell me. The first need this project attended was allowing a Python assertion library to implement a similar API to RSpec Expectations and should.js. But people got creative and used it to among other things spy on things or to integrate profiling.
Tiny Example
It basically allows you to patch built-in objects, declared in C through python. Just like this:
- Add a new method to the
intclass:
from forbiddenfruit import curse
def words_of_wisdom(self):
return self * "blah "
curse(int, "words_of_wisdom", words_of_wisdom)
assert (2).words_of_wisdom() == "blah blah "
- Add a
classmethodto thestrclass:
from forbiddenfruit import curse
def hello(self):
return "blah"
curse(str, "hello", classmethod(hello))
assert str.hello() == "blah"
Reversing a curse
If you want to free your object from a curse, you can use the reverse()
function. Just like this:
from forbiddenfruit import curse, reverse
curse(str, "test", "blah")
assert 'test' in dir(str)
# Time to reverse the curse
reverse(str, "test")
assert 'test' not in dir(str)
Beware: reverse() only deletes attributes. If you curse()'d to replace
a pre-existing attribute, reverse() won't re-install the existing attribute.
Context Manager / Decorator
cursed() acts as a context manager to make a , and then
it on exit. It uses
,
so on Python 3.2+ it can also be used as a function decorator. Like so:
