[Python-Dev] defaultproperty (was: Re: RFC: readproperty)

Jim Fulton jim at zope.com
Sun Oct 9 18:33:12 CEST 2005


Based on the discussion, I think I'd go with defaultproperty.

Questions:

- Should this be in builtins, alongside property, or in
   a library module? (Oleg suggested propertytools.)

- Do we need a short PEP?

Jim

Jim Fulton wrote:
> Guido van Rossum wrote:
> 
>>On 9/28/05, Jim Fulton <jim at zope.com> wrote:
>>
> 
> ...
> 
>>I think we need to be real careful with chosing a name -- in Jim's
>>example, *anyone* could assign to Spam().eggs to override the value.
>>The name "readproperty" is too close to "readonlyproperty",
> 
> 
> In fact, property creates read-only properties for new-style classes.
> (I hadn't realized, until reading this thread, that for classic
> classes, you could still set the attribute.)
> 
>  > but
> 
>>read-only it ain't! "Lazy" also doesn't really describe what's going
>>on.
> 
> 
> I agree.
> 
> 
>>I believe some folks use a concept of "memo functions" which resemble
>>this proposal except the notation is different: IIRC a memo function
>>is always invoked as a function, but stores its result in a private
>>instance variable, which it returns upon subsequent calls. This is a
>>common pattern. Jim's proposal differs because the access looks like
>>an attribute, not a method call. Still, perhaps memoproperty would be
>>a possible name.
>>
>>Another way to look at the naming problem is to recognize that the
>>provided function really computes a default value if the attribute
>>isn't already set. So perhaps defaultproperty?
> 
> 
> Works for me.
> 
> Oleg Broytmann wrote:
>  > On Wed, Sep 28, 2005 at 10:16:12AM -0400, Jim Fulton wrote:
>  >
>  >>   class readproperty(object):
>  >
>  > [skip]
>  >
>  >>I do this often enough
>  >
>  >
>  >    I use it since about 2000 often enough under the name CachedAttribute:
>  >
>  > http://cvs.sourceforge.net/viewcvs.py/ppa/qps/qUtils.py
> 
> Steven Bethard wrote:
>  > Jim Fulton wrote:
>  >
> ...
>  > I've also needed behavior like this a few times, but I use a variant
>  > of Scott David Daniel's recipe[1]:
>  >
>  > class _LazyAttribute(object):
> 
> 
> Yup, the Zope 3 sources have something very similar:
> 
> http://svn.zope.org/Zope3/trunk/src/zope/cachedescriptors/property.py?view=markup
> 
> I actually think this does too much.  All it saves me, compared to what I proposed
> is one assignment.  I'd rather make that assignment explicit.
> 
> Anyway, all I wanted with readproperty was a property that implemented only
> __get__, as opposed to property, which implements __get__, __set__, and __delete__.
> 
> I'd be happy to call it readproprty or getproperty or defaulproperty or whatever. :)
> 
> I'd prefer that it's semantics stay fairly simple though.
> 
> 
> Jim
> 


-- 
Jim Fulton           mailto:jim at zope.com       Python Powered!
CTO                  (540) 361-1714            http://www.python.org
Zope Corporation     http://www.zope.com       http://www.zope.org


More information about the Python-Dev mailing list