20

Possible Duplicate:
Can I add custom methods/attributes to built-in Python types?

In Ruby you can override any built-in object class with custom method, like this:

class String
  def sayHello
    return self+" is saying hello!"
  end
end                              

puts 'JOHN'.downcase.sayHello   # >>> 'john is saying hello!'

How can i do that in python? Is there a normally way or just hacks?

2
  • 1
    This has just been answered in stackoverflow.com/questions/4698493/… . Suggest closing. Commented Jan 15, 2011 at 10:57
  • Monkeypatching is possible but has limits, as pointed out in that question (and even more). I recommend just defining a "free" function that does this. Commented Jan 15, 2011 at 11:09

2 Answers 2

29

You can't because the builtin-types are coded in C. What you can do is subclass the type:

class string(str):
    def sayHello(self):
        print(self, "is saying 'hello'")

Test:

>>> x = string("test")
>>> x
'test'
>>> x.sayHello()
test is saying 'hello'

You could also overwrite the str-type with class str(str):, but that doesn't mean you can use the literal "test", because it is linking to the builtin str.

>>> x = "hello"
>>> x.sayHello()
Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    x.sayHello()
AttributeError: 'str' object has no attribute 'sayHello'
>>> x = str("hello")
>>> x.sayHello()
hello is saying 'hello'
Sign up to request clarification or add additional context in comments.

Comments

2

The normal Python equivalent to this is to write a function that takes a string as it's first argument:

def sayhello(name):
    return "{} is saying hello".format(name)

>>> sayhello('JOHN'.lower())
'john is saying hello'

Simple clean and easy. Not everything has to be a method call.

3 Comments

The disadvantage of this approach is that you can't write a mutator method. For example, today I want a method str.startswithThenRemove(p) that mutates str to remove the optional prefix p and then returns True if the prefix was found and False if it wasn't: if option.startswithThenRemove("--foo="): handleFoo(option). You can't do that in any easy way in Python (but see stackoverflow.com/a/1551223/1424877).
Even if you could add a custom method to a string you still wouldn't be able to write a mutator method: Python strings are immutable.
Except this method is neither clean not easy. Ugly af, at least compared to what OP provided

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.