[Tutor] Design - getFoo and setFoo methods
Lloyd Kvam
pythontutor@venix.com
Thu, 16 May 2002 08:25:09 -0400
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.
Alexandre Ratti wrote:
> Hello,
>
>
> When do you use getFoo() and setFoo() methods in your classes? (I think
> there can be called "accessors"?).
>
> The question came up on c.l.py a couple of days ago and most
> participants said they did not usually use them, eg. they access
> attributes directly.
>
> => Do you only use get and set methods when data needs some extra work
> before it can be stored or returned?
>
> => If yes, do you use the _ convention in attribute names to remember
> when attributes can be accessed directly? Eg. self.foo can be read and
> written directly, but self._foo should not.
>
> So far I used many set and get methods, but now I wonder whether they
> really make sense when data is not somehow transformed before
> storing/returning.
>
>
> Thanks again.
>
> Alexandre
>
>
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
--
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358
voice:
603-443-6155
fax:
801-459-9582