Tutorial :Can you monkey patch methods on core types in Python?

Question:

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.

But you can't do this for certain built-in classes, like int or float. These classes' methods are implemented in C and there are certain abstractions sacrificed in order to make the implementation easier and more efficient.

I'm not really clear on why you would want to alter the behavior of the built-in numeric classes anyway. If you need to alter their behavior, subclass them!!

Solution:2

No, you cannot. In Python, all data (classes, methods, functions, etc) defined in C extension modules (including builtins) are immutable. This is because C modules are shared between multiple interpreters in the same process, so monkeypatching them would also affect unrelated interpreters in the same process.

However, classes defined in Python code may be monkeypatched because they are local to that interpreter.

Solution:4

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

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 a tuple 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 in item.price (or elsewhere) would be very likely to be considered the Pythonic thing to do. I can easily imagine somebody deciding to add a price_should_equal() method to item 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 in

should_equal(observed=item.price, expected=19.99)

or 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 just int or float. But perhaps I'm getting too caught up in the details of the particular example that you happened to provide.

Solution:6

You can't patch core types in python. However, you could use pipe to write a more human readable code:

Solution:8

If you really really really want to do a monkey patch in Python, you can do a (sortof) hack with the "import foo as bar" technique.

If you have a class such as TelnetConnection, and you want to extend it, subclass it in a separate file and call it something like TelnetConnectionExtended.

Then, at the top of your code, where you would normally say:

import TelnetConnection

change that to be:

import TelnetConnectionExtended as TelnetConnection

and then everywhere in your code that you reference TelnetConnection will actually be referencing TelnetConnectionExtended.

Sadly, this assumes that you have access to that class, and the "as" only operates within that particular file (it's not a global-rename), but I've found it to be useful from time to time.

Solution:9

No, sadly you cannot extend types implemented in C at runtime.

You can subclass int, although it is non-trivial, you may have to override __new__.

You also have a syntax issue:

1.somemethod() # invalid

However

(1).__eq__(1) # valid

Solution:10

No, you can't do that in Python. I consider it to be a good thing.

Solution:11

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.

Solution:12

What does should_equal do? Is it a boolean returning True or False? In that case, it's spelled:

item.price == 19.99

There's no accounting for taste, but no regular python developer would say that's less readable than your version.

Does should_equal instead set some sort of validator? (why would a validator be limited to one value? Why not just set the value and not update it after that?) If you want a validator, this could never work anyway, since you're proposing to modify either a particular integer or all integers. (A validator that requires 18.99 to equal 19.99 will always fail.) Instead, you could spell it like this:

item.price_should_equal(19.99)

or this:

item.should_equal('price', 19.99)

and define appropriate methods on item's class or superclasses.

Solution:13

It seems what you really wanted to write is:

assert item.price == 19.99

(Of course comparing floats for equality, or using floats for prices, is a bad idea, so you'd write assert item.price == Decimal(19.99) or whatever numeric class you were using for the price.)

You could also use a testing framework like py.test to get more info on failing asserts in your tests.