getattr and setattr

Dale Strickland-Clark dale at out-think.NOSPAMco.uk
Thu Jul 6 08:58:45 EDT 2000


I'm still trying to get to grips with object programming in Python and
finding decent documentation unbelievably hard to track down.

I eagerly set about the tutorial but found it was  lacking in - well - just
about every department.

I can't find any meat on __getattr__ and __setattr__.

These two special object functions don't seem to be very well documented
anywhere. (Please prove me wrong!)

I can see how to handle a special case in __getattr__ with something like:

class wibble
        def __getattr__(name):
                if name == 'fred':
                        return 1
                else:
                        # what?

But what if it's not the special case(s). How do I return an arbitrary
attribute? Do I do something like:

            return self.__dict__[name]

Or is there something a bit more transparent?

Any help, as always, gratefully received.

--
Dale Strickland-Clark
Out-Think Ltd, UK
Business Technology Consultants






More information about the Python-list mailing list