[Tutor] Design - getFoo and setFoo methods
Sean 'Shaleh' Perry
shalehperry@attbi.com
Thu, 16 May 2002 07:55:23 -0700 (PDT)
On 16-May-2002 Lloyd Kvam wrote:
> It is easy in python to catch changes to attributes in the __setattr__
> method.
> This allows you to do any special processing while providing the appearance
> of a simple assignment. So I do not write special setFoo / getFoo methods.
>
> Python 2.2 now supports a new type called property designed to further
> simplify
> get/set processing. (I have not updated my code to adopt this yet.)
> http://www.amk.ca/python/2.2/index.html#SECTION000340000000000000000
> What's New in Python 2.2
>
> I do use the _name convention to indicate properties that should not be
> touched
> directly from outside the class.
>
indeed, Python style is for fast writing and testing. Having to create lots of
boiler plate functions:
getFoo(self): return foo
getBar(self): return bar
goes against this. So general get/sets are used when giving direct access to
the data would be bad.
Note that this design idea changes a little as the code use changes. The more
likely the object is to be used by a random python coder the more likely the
object will be wrapped in protective code.
I seem to recall _foo doing some magic in the class namespace to help hide the
variable.