Automatic Generation of Python Class Files

Steven Bethard steven.bethard at gmail.com
Mon Oct 22 19:31:51 EDT 2007


Bruno Desthuilliers wrote:
> Steven Bethard a écrit :
> (snip)
>> In Python, you can use property() to make method calls look like 
>> attribute access.  This could be necessary if you have an existing API 
>> that used public attributes, but changes to your code require those 
>> attributes to do additional calculations now.
>>
>> But if you're creating a class for the first time, it should *never* 
>> use property().  There's no need to retrofit anything.
> 
> May I kindly disagree here ?-)

Of course. ;-)

> Computed attributes are IMHO not only a life-saver when it comes to 
> refactoring. There are cases where you *really* have - by 'design' I'd 
> say - the semantic of a property, but know from the start you'll need 
> computation (for whatever reason). Then what would be the rationale for 
> using explicit getters/setters ?

I'd be interested to hear what these use cases are.

Generally, people expect that attributes are used for simple state, and 
methods are used when some sort of computation is required. If you use 
attributes when some computation is required, you violate your users' 
expectations.

Of course, you can always document that your attributes require 
computation. But you're definitely adding some cognitive load by 
violating the normal expected behavior of attributes and methods.

STeVe



More information about the Python-list mailing list