to property or function in class object
steven.bethard at gmail.com
Fri Aug 17 19:43:35 CEST 2007
> 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.
> While both approach would get the job done, I don't know much about
> the performance, benefits, design philosophy between the two approach.
I tend to view property as only something you use for the purposes of
backwards compatibility. So if you started with something as a simple
attribute and you later realize that it needs to do some computation,
you can use properties to keep the same API.
However, if I know right off the bat that I need to do some computation
to return a result, then I'll always prefer a method. People expect a
method call to take some computation. People don't always expect that
from a simple attribute access. So I think it's more predictable for
your users if you use a method when there's any real work being done.
More information about the Python-list