修补python内置对象
forbiddenfruit的Python项目详细描述
#禁果
啊![禁果](logo.png)
这个项目的目的是帮助你在写测试的时候到达天堂,但是 如果用在生产代码上,可能会把你引向地狱。
它基本上允许您修补在c到 Python.就像这样:
`python >>> 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 " `
砰!就这样,你的int类现在有了智慧的words方法。做 您想将classmethod添加到内置类吗?没问题,就这样做:
`python >>> from forbiddenfruit import curse >>> def hello(self): ... return "blah" >>> curse(str, "hello", classmethod(hello)) >>> assert str.hello() == "blah" `
###扭转诅咒
如果您想将对象从诅咒中解放出来,可以使用reverse() 功能。就像这样:
`python >>> 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) `
##兼容性
到目前为止,我测试过的所有cpython版本上都运行了禁果,其中包括 版本2.5、2.6、2.7、3.2和3.3。因为禁果从根本上来说 依赖于c api,这个库不能在其他python上工作 实现,如Jython、Pypy等
我可能会在将来添加对pypy的支持,但我不太可能这样做 为Jython。但我可以很高兴地接受他们的补丁。
##许可证
许可证(GPLv3)
版权所有(c)2013 Lincoln Clarete<;lincoln@comum.org>;
此程序是免费软件:您可以重新分发和/或修改 根据由 自由软件基金会,或者许可证的第3版,或者 (由您选择)任何更高版本。
这个程序的发布是希望它能有用, 但没有任何保证;甚至没有 适销性或适合某一特定目的的适销性。见 GNU通用公共许可证了解更多详细信息。
你应该收到GNU通用公共许可证的副本 还有这个节目。如果没有,请参见<;http://www.gnu.org/licenses/>;。
###徽标由
金伯利·钱德勒,来自名词项目