Python kernel types are immutable in design, as other users point out:
>>> int.frobnicate = lambda self: whatever() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can't set attributes of built-in/extension type 'int'
Of course, you could achieve the effect that you describe by creating a subclass, since custom types in Python change by default.
>>> class MyInt(int): ... def frobnicate(self): ... print 'frobnicating %r' % self ... >>> five = MyInt(5) >>> five.frobnicate() frobnicating 5 >>> five + 8 13
There is no need to publish a subclass of MyInt public; You can precisely define it directly in the function or method that creates the instance.
There are, of course, several situations where Python programmers who are fluent in the idiom believe that this subclass class is correct. For example, os.stat() returns a subclass of tuple that adds named members, just to address the issue of readability, which you are talking about in your example.
>>> import os >>> st = os.stat('.') >>> st (16877, 34996226, 65024L, 69, 1000, 1000, 4096, 1223697425, 1223699268, 1223699268) >>> st[6] 4096 >>> st.st_size 4096
However, in the specific example you are giving, I do not believe that subclassing float in item.price (or elsewhere) will be very likely to be considered a pythonic task. I can easily imagine that someone decided to add the price_should_equal() method to item , if that was the main use case; if someone were looking for something more general, it might be wiser to use named arguments to make the intended value more clear, as in
should_equal(observed=item.price, expected=19.99)
or something like that. This is a bit verbose, but no doubt it could be improved. A possible advantage of this approach over a ruby monkey patch is that should_equal() can easily perform comparisons of any type, not just int or float . But maybe I'm too fixated on the details of the specific example that was provided to you.