Ruby can add methods to the Number class and other core types to get effects like this:
1.should_equal(1)
But it seems like Python cannot do this. Is this true? And if so, why? Does it have something to do with the fact that type can't be modified?
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.
To answer some of you: The reason I might want to do this is simply aesthetics/readability.
item.price.should_equal(19.99)
This reads more like English and clearly indicates which is the tested value and which is the expected value, as supposed to:
should_equal(item.price, 19.99)
This concept is what Rspec and some other Ruby frameworks are based on.
Have fun ;)
Python's core types are immutable by design, as other users have pointed out:
You certainly could achieve the effect you describe by making a subclass, since user-defined types in Python are mutable by default.
There's no need to make the
MyInt
subclass public, either; one could just as well define it inline directly in the function or method that constructs the instance.There are certainly a few situations where Python programmers who are fluent in the idiom consider this sort of subclassing the right thing to do. For instance,
os.stat()
returns atuple
subclass that adds named members, precisely in order to address the sort of readability concern you refer to in your example.That said, in the specific example you give, I don't believe that subclassing
float
initem.price
(or elsewhere) would be very likely to be considered the Pythonic thing to do. I can easily imagine somebody deciding to add aprice_should_equal()
method toitem
if that were the primary use case; if one were looking for something more general, perhaps it might make more sense to use named arguments to make the intended meaning clearer, as inor something along those lines. It's a bit verbose, but no doubt it could be improved upon. A possible advantage to such an approach over Ruby-style monkey-patching is that
should_equal()
could easily perform its comparison on any type, not justint
orfloat
. But perhaps I'm getting too caught up in the details of the particular example that you happened to provide.You can do this, but it takes a little bit of hacking. Fortunately, there's a module now called "Forbidden Fruit" that gives you the power to patch methods of built-in types very simply. You can find it at
http://clarete.github.io/forbiddenfruit/?goback=.gde_50788_member_228887816
or
https://pypi.python.org/pypi/forbiddenfruit/0.1.0
With the original question example, after you write the "should_equal" function, you'd just do
and you're good to go! There's also a "reverse" function to remove a patched method.
You can't patch core types in python. However, you could use pipe to write a more human readable code:
Here's how I achieve the .should_something... behavior:
or
I included a decorator method for extending this behavior at runtime on a stand-alone method:
You have to know a bit about the internals but it works.
Here's the source:
https://github.com/mdwhatcott/pyspecs
It's also on PyPI under pyspecs.
No but you have UserDict UserString and UserList which were made with exactly this in mind.
If you google you will find examples for other types, but this are builtin.
In general monkey patching is less used in Python than in Ruby.