Ruby可以向Number类和其他核心类型添加方法,以获得如下效果:
1.should_equal(1)
但Python似乎无法做到这一点.这是真的吗?如果是,为什么?这与type不能被修改这一事实有关吗?
Update: Rather than talking about different definitions of monkey patching, I would like to just focus on the example above. I have already concluded that it cannot be done as a few of you have answered. But I would like a more detailed explanation of why it cannot be done, and maybe what feature, if available in Python, would allow this.
回答你们中的一些人:我想这样做的原因只是美观/可读性.
item.price.should_equal(19.99)
这读起来更像英语,清楚地表明哪一个是测试值,哪一个是预期值,应该是:
should_equal(item.price, 19.99)
这个概念是Rspec和其他一些Ruby框架的基础.