From: Jim Freeze Date: 2002-09-19T22:20:34+09:00 Subject: Re: Is better to subclass or to add methods to an existing class? On Thu, Sep 19, 2002 at 09:20:13PM +0900, Vincent Foley wrote: > > 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: > [snippage] > > 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 think it could be considered bad because it can produce alergic reactions between libraries. For example: --- LibA class String def rot13 tr(A-Za-z,...) end end --- LibB class String def rot13 puts "rotate 13 degrees" end end ---- require liba require libb "fred".rot13 #=> this will probably not do what you want it to. Since both libraries have redefined a core class and have added a method with the same name, there will ultimately be a problem in the code. This is why I avoid modifying core libraries and I hope other library writers do as well. -- Jim Freeze ---------- Programming Ruby def initialize; fun; end A language with class