I want to add custom properties to the standard Python types (e.g. a size
property to the str
type), but the code below doesn't seem to do the trick; I get AttributeError: 'str' object has no attribute 'size'
. What's wrong?
import __builtin__
class str2(str):
def __init__(self, text):
super(str2, self).__init__(text)
self.size = len(self)
__builtin__.str = str2
print str2("foo").size # This works
print str("bar").size # This also works
print "baz".size # But this doesn't