修补python内置对象
forbiddenfruitinit的Python项目详细描述
#禁果
啊!【禁果】(徽标.png)在
这个项目的目的是帮助你在写测试时达到天堂,但是它 如果用在生产代码上,可能会让你下地狱。在
它基本上允许你修补内置对象,通过C声明 Python。就像这样:
`python >>> from forbiddenfruitinit 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_-of-wish方法。做 是否要将classmethod添加到内置类?没问题,只要这样做:
`python >>> from forbiddenfruitinit import curse >>> def hello(self): ... return "blah" >>> curse(str, "hello", classmethod(hello)) >>> assert str.hello() == "blah" `
逆转诅咒
如果你想让你的对象免于诅咒,你可以使用reverse() 功能。就像这样:
`python >>> from forbiddenfruitinit 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>
这个程序是免费软件:你可以重新发布和/或修改 根据GNU通用公共许可证的条款 自由软件基金会,许可证的第3版,或 (由您选择)任何更高版本。在
分发这个程序是希望它能有用, 但没有任何保证;甚至没有 适销性或对特定目的的适用性。见 GNU通用公共许可证获取更多详细信息。在
您应该已经收到了GNU通用公共许可证的副本 和这个节目一起。如果没有,请参见<;http://www.gnu.org/licenses/>;。在
标志由
金伯利·钱德勒,来自名词项目
[![比特德利徽章](https://d2weczhvl823v0.cloudfront.net/clarete/forbiddenfruit/trend.png)](https://bitdeli.com/free“比特德利徽章”)
- 项目
标签: