PEP 8 and properties

Ian Bicking ianb at colorstudy.com
Sun Jan 26 22:13:24 EST 2003


On Sun, 2003-01-26 at 18:40, Greg Ewing (using news.cis.dfn.de) wrote:
> Ian Bicking wrote:
> 
>  > On Mon, 2003-01-20 at 20:41, John Roth wrote:
>  >
>  > I feel like there's other cases I haven't come up with, where the rules
>  > I've decided on so far don't apply, or they do but I disagree with them
>  > for that case.
> 
> 
> One case I'm particularly interested in is things like the
> position and size of a widget in a GUI. This is something
> I'll be thinking about when I get back to my Python GUI
> project. Currently I'm using accessor methods for these,
> and I'll have to decide whether to switch over to using
> properties.
> 
> What do people think about these? Changing them has some
> pretty obvious side effects, yet it still seems easy to
> see them as attributes that can be examined and changed.
> Are they side-effectish enough that they shouldn't
> be properties?

Good point -- I should say that "side-effect" is only bad if that's when
you're accessing the attribute.  Setting the attribute *always should*
have a side effect, though that might only be a side effect of having a
different value for the attribute.  

It's entirely possible, for instance, that your widget polls the
attribute and updates its position regularly.  Of course this is
inefficient, but it's valid.  There's all sorts of situations where it's
hard to distinguish if the attribute's change is reflected elsewhere
because some other code is looking at that attribute, or if changing the
attribute directly triggers a change elsewhere.  This is *exactly* what
property is meant to be used for, to allow refactoring from one
situation to another.

It's still valid to use methods for all of it, but if you want to use
property then coordinates would be a good place to start.

Maybe it would be more confusing if you chopped off coordinates (to fit
the widget its container, for instance).  Then you could get something
like
    >>> widget.x = 1000
    >>> widget.x
    500
and this would seem disturbing.  But then I find it disturbing if that
happens even with methods, if the methods don't give the impression you
are asking the object to do something, not telling it so.  I'd expect an
exception in either case.

But if the object moves on its own that's probably okay.  That happens
all the time, even without using properties.


-- 
Ian Bicking           Colorstudy Web Development
ianb at colorstudy.com   http://www.colorstudy.com
PGP: gpg --keyserver pgp.mit.edu --recv-keys 0x9B9E28B7
4869 N Talman Ave, Chicago, IL 60625 / (773) 275-7241






More information about the Python-list mailing list