PDA

View Full Version : Streamlining Accessors in Python



Ubuntist
October 20th, 2006, 10:52 AM
I'm a big fan of OO but get tired of having to write accessor methods all the time. Basic read and write accessors are so simple and so commonly used that I think it would be great if Python had a shortcut for creating them. I know Ruby has such a shortcut built into the language itself. Is there some way of faking the same thing in Python?

Wille
October 20th, 2006, 12:36 PM
You probably shouldn't use accessor functions in python. Just use attributes directly.

Later on, if you want to make attribute "electric" (so that get/set has side effects), you can use the "property" feature.

Though there seems to be a recipe that might interest you:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/157768

Ubuntist
October 20th, 2006, 03:12 PM
Thanks -- that's cool piece of code!