PEP 8 addition: Preferred property style?

Hello, 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? Gerrit [0] http://tinyurl.com/2rydd

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/)

Hi, [Gerrit Holl]
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?
[Guido van Rossum]
I don't like it, and I'd rather not see this become a standard idiom.
OK.
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.
I was thinking of it as analogue to classmethod() and staticmethod(), but it isn't, of course. The getter/setter are still accesible through the property object itself, but I get your point. I think there is a problem with the current way of defining properties. A property commonly exists of several parts. These parts form one object. But the parts are not grouped by indentation. In my opinion, readability would be enhanced if they would. One way to do this is adding syntax - discussed about a year ago with no conclusion. Another way is to use a class with a metaclass - not clean, because you aren't creating a class after all. I thought this would be a good concession, but it has similar disadvantages. Do you agree that there is a a problem here? Should Guido van Rossum's opinion be included in PEP 8? yours, Gerrit.

I think there is a problem with the current way of defining
[Gerrit] properties.
A property commonly exists of several parts. These parts form one object. But the parts are not grouped by indentation. In my opinion, readability would be enhanced if they would. One way to do this is adding syntax - discussed about a year ago with no conclusion. Another way is to use a class with a metaclass - not clean, because you aren't creating a class after all. I thought this would be a good concession, but it has similar disadvantages.
Do you agree that there is a a problem here?
I don't agree. The get/set/del functions can be defined anywhere (even outside the class definition). It is the line where property() is called that everything comes together and that works out fine, both in theory and practice. not-everything-needs-special-syntax-ly yours, Raymond Hettinger

Hello,
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
"Gerrit Holl" <gerrit@nl.linux.org> wrote in message news:20040122200344.GA3185@nl.linux.org... 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:
The 'cute' is a mental burden for the naive reader. For compactness and no extra name bindings, I might prefer def mtime(): # temp binding to setter return os.utime(str(self), (self.atime, t)) #'return expr' is atypical mtime = property( "Modification time", lambda: os.path.getmtime(str(self)), mtime) But I like 'def _set():...', with 'mtime =...' followed either by 'del _set' or another 'def _set():...' even better. Terry J. Reedy
participants (4)
-
Gerrit Holl
-
Guido van Rossum
-
Raymond Hettinger
-
Terry Reedy