Extension without sub-classing?

Ben Finney bignose-hates-spam at and-zip-does-too.com.au
Mon Jun 2 22:55:11 EDT 2003


On Tue, 03 Jun 2003 02:17:35 GMT, Martin d'Anjou wrote:
> Is it possible to extend a python class without sub-classing?

AFAIK, no.

Why do you want to?  I can't see that it buys you anything, and it
causes all kinds of confusion when trying to use a class spread across
multiple files.

What is it you don't like about subclassing to extend functionality?


class car:
    def start_engine( self ):
        print "Engine started"

class car_with_brakes( car ):
    def apply_brakes( self ):
        print "Car stopped"

>>> vehicle = car_with_brakes()
>>> vehicle.start_engine()
Engine started
>>> vehicle.apply_brakes()
Car stopped
>>>

-- 
 \     "Homer, where are your clothes?"  "Uh... dunno."  "You mean Mom |
  `\   dresses you every day?!"  "I guess; or one of her friends."  -- |
_o__)                                     Lisa & Homer, _The Simpsons_ |
http://bignose.squidly.org/ 9CFE12B0 791A4267 887F520C B7AC2E51 BD41714B




More information about the Python-list mailing list