I was discussing with a (Python) friend last night.  I told him that one
thing I liked better about Ruby than Python was that you could add
methods to already existing methods.  For instance, if I wanted to add a
rot13 method to the String class, all I have to do is this:

[code]
class String
  def rot13
    tr("A-Za-z", "N-ZA-Mn-za-m")
  end
end

"foobar".rot13
[/code]

But my friend told me that Python didn't have that because it was not a
good thing and it was not the proper way to do it.  He said that the
true way of doing it, is to subclass (since Python 2.2 can now subclass
builtin types) the base class:

[code]
class myStr(str):
  def rot13(self):
      trans = maketrans(
                  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
                  "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm")
      newstring = translate(word, trans)
      return newstring

myStr("foobar").rot13()
[/code]

or in Ruby
[code]
class MyStr < String
  def rot13
    tr("A-Za-z", "N-ZA-Mn-za-m")
  end
end

MyStr.new("foobar").rot13
[/code]

His main argument was just that, "It's the Wrong Way (TM) to do it".  To
me, it just seems like extra code and added complexity.

Can you enligthen me and tell me if it's really that bad an idea to add
methods to an existing class, or if he's just being a Python zealot?

I'd also like to know if other OO languages (SmallTalk, Eiffel, Sather,
etc.) allow class modifications.

Cheers,

Vince

-- 

Vincent Foley-Bourgon
Email: vinfoley / iquebec.com
Homepage: http://darkhost.mine.nu:81