Classes

Chris Angelico rosuav at gmail.com
Sat Nov 1 07:32:09 CET 2014


On Sat, Nov 1, 2014 at 5:12 PM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> On Fri, Oct 31, 2014 at 7:06 PM, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info> wrote:
>> And there are times when using getters and setters is the right choice.
>> Properties should only be used for quite lightweight calculations, because
>> attribute access is supposed to be fast. If your calculation is complex,
>> time-consuming or might fail, using a property is a bad idea and you should
>> use an explicit getter method, possibly with a setter if needed.
>
> I agree except that in this scenario you should probably use a verb
> other than "get", since getters should also be fast.

Maybe. "get_track_length()" might be a reasonable method name for
something that opens an audio file, reads its header, and returns its
length in seconds. It should be a method rather than a property
(attribute access should be fast), but "get" makes sense here, as it's
not calculating or anything, it's just retrieving information. But
yes, if it's a calculation, "calc" would be more common.

ChrisA



More information about the Python-list mailing list