[Python-Dev] PEP 8 addition: Preferred property style?

Guido van Rossum guido at python.org
Thu Jan 22 15:11:11 EST 2004


> currently, PEP 8 does not have any advice about the preferred style
> in defining properties. There are only two places in the standard
> library where they are used (socket.py and xml/dom/minicompat.py), so
> that doesn't provide much information either. Personally, I don't like the
> namespace cluttering caused by defining __get, __set and/or __del. Since
> I saw someone[0] on c.l.py using it, I always use this style:
> 
>     def mtime():
>         doc = "Modification time"
> 
>         def get(self):
>             return os.path.getmtime(str(self))
> 
>         def set(self, t):
>             return os.utime(str(self), (self.atime, t))
> 
>         return get, set, None, doc
>     mtime = property(*mtime())
> 
> I like it, because it's very readable for me, and doesn't clutter the
> namespace. Two questions about this style:
> 
>     - Is it tolerable to use it in the standard library (PrePEP)?
>     - Should it be advised for or against in PEP 8, or neither?

I don't like it, and I'd rather not see this become a standard idiom.

Personally, I don't see the issue with the namespace cluttering
(what's bad about it?) and I actually like that the getter/setter
functions are also explicitly usable (hey, you might want to use one
of these as the key argument to sort :-).

My objection against the def is that it requires a lot of
sophistication to realize that this is a throw-away function, called
just once to return argument values for the property call.  It might
also confuse static analyzers that aren't familiar with this pattern.

--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-Dev mailing list