From: michael libby Date: 2002-09-19T22:02:24+09:00 Subject: Re: Is better to subclass or to add methods to an existing class? -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Thursday 19 September 2002 07:20, Vincent Foley wrote: [example modification of existing class snipped] > 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: [python code removed... washing hands...] > 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. It is the Right Way To Do It if your code passes all its unit and acceptance tests. It is the Wrong Way To Do It if your code fails to perform as expected. Beyond that, who cares? You might perform some trivial benchmark to compare the two on a variety of base classes and a variety of common "extensions" and reach some overall efficiency conclusion, though. > 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? Nope, can't say. ;) Personally if I have a function that takes a string and only a string (like the example Rot13) then I would extend the String class. Otherwise, while programming, I have to remember if my strings (lowercase) are Strings or MyStrings in order to remember which methods they have access to. But it depends on the application. Maybe in some cases I want two separate kinds of strings.... kind of like how we have lots of separate kinds of numbers. -michael ++++++++++++++++++++++++++++++++++++++++++ Michael C. Libby public key: http://www.ichimunki.com/public_key.txt web site: http://www.ichimunki.com ++++++++++++++++++++++++++++++++++++++++++ -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.6 (GNU/Linux) Comment: For info see http://www.gnupg.org iD8DBQE9icn84ClW9KMwqnMRAuj1AJ0XBOJMnFZdB9FRrvCHHQ8ls4310QCgo7/c 5cwfBokSCtha2DjtQ6KdqcQ= =uoZ/ -----END PGP SIGNATURE-----