Can I run an operation on an object's attribute when reading?

Ben Finney bignose+hates-spam at benfinney.id.au
Mon Jan 19 07:33:14 EST 2009


Phillip B Oldham <phillip.oldham at gmail.com> writes:

> Thanks, but I'm looking for a way to do it *without* using a getter
> as I don't have easy access to the class (its being generated for me
> elsewhere). Essentially I'd like to overwrite (if possible) the
> default behavior when returning certain attributes on certain
> objects.

You can override the behaviour of a class by defining a subclass.

    class UntouchableTypeWrapper(UntouchableType):

        def _foo_get(self):
            return str(self.foo)

        foo = property(_foo_get)

Then you can create instances of the wrapper class where you want the
new behaviour.

-- 
 \          “… a Microsoft Certified System Engineer is to information |
  `\     technology as a McDonalds Certified Food Specialist is to the |
_o__)                               culinary arts.” —Michael Bacarella |
Ben Finney



More information about the Python-list mailing list