Automatic Generation of Python Class Files

Steven Bethard steven.bethard at gmail.com
Tue Oct 23 13:35:39 EDT 2007


Bruno Desthuilliers wrote:
> Steven Bethard a écrit :
>> 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.
> 
> I once wrote a small ORM-like wrapper for LDAP access, and, for reasons 
> that might be obvious for anyone having worked with LDAP, I choosed to 
> keep the record values in the format used by the lower level LDAP lib 
> and let user code access them thru computed attributes (actually 
> custom-made descriptors).

But this is trying to match an existing API, the only case that I think 
you *should* be using property().

> I could also list the CS101 examples, like Shape.area, 
> Rect.bottom_right, Person.age, etc... And yes, some of these attributes 
> are obviously read-only. That doesn't prevent them from being 
> semantically *properties*, not *behaviour*.

But as I mentioned in another email here, I'd rather know which ones 
require computation, on the off chance that even the small amount of 
additional calculation matters (say, for large integers or in a very 
tight loop).

I guess as long as your documentation is clear about which attributes 
require computation and which don't...

STeVe



More information about the Python-list mailing list