to property or function in class object

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Fri Aug 17 02:19:34 EDT 2007


On Fri, 17 Aug 2007 02:29:47 +0000, james_027 wrote:

> i am very new to python, not knowing much about good design. I have an
> object here for example a Customer object, where I need to retrieve a
> info which has a number of lines of code to get it.
> 
> my question is weather what approach should I use? to use the property
> which is from the python new class style as I understand or simple use
> function that will return the the info I needed.

I'd say it's a matter of taste here.  Where `get_*` and `set_*` methods
should not be used is getting and setting simple attributes.  Here
properties are very handy if such a simple attribute changes to something
calculated later on, because it is possible to change the class without
the need to change the code that uses such objects.

In the other case, more complex code to get or set some information in
the first place, it is a matter of taste IMHO.  Ask yourself if the user
would expect `balance` to be an attribute of that class.  If it seems to
be "natural" to have such an attribute.  And usually attribute access does
not trigger really heavy computation.  Constructing the answer from some
different attributes or doing a conversion before returning something is
okay, but a calculation that lasts an hour or so would surprise many.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list