Overriding a method in an instance

Kalle Svensson kalle at gnupung.net
Wed May 2 08:23:03 EDT 2001


Sez Martyn Quick:
> I was wondering what the correct way to change the definition of a method
> of an instance of a class is.  The following seems to work:

Well, that depends on what you mean by correct.  IMHO, the *correct* way
would be to create a subclass (or use delegation):

class TestClass:
    def func(self):
        print "1"

class TestSubClass(TestClass):
    def func(self):
        print "2"

instance = TestSubClass()

I'm not sure about your situation, of course.  Perhaps you can present a
compelling reason to change methods on instances without subclassing (or
delegation)?

Peace,
  Kalle
-- 
Email: kalle at gnupung.net     | You can tune a filesystem, but you
Web: http://www.gnupung.net/ | can't tune a fish. -- man tunefs(8)
PGP fingerprint: 0C56 B171 8159 327F 1824 F5DE 74D7 80D7 BF3B B1DD
 [ Not signed due to lossage.  Blame Microsoft Outlook Express. ]




More information about the Python-list mailing list