[Python-3000] hash as attribute/property

Brett Cannon brett at python.org
Tue Apr 4 21:48:43 CEST 2006


On 4/4/06, Barry Warsaw <barry at python.org> wrote:
> On Tue, 2006-04-04 at 09:57 -0700, Guido van Rossum wrote:
> > On 4/3/06, Delaney, Timothy (Tim) <tdelaney at avaya.com> wrote:
> > > I've been thinking that `hash` could be an attribute (or property if it
> > > needs to be calculated on-the-fly) rather than the current method call.
> > >
> > > Or it could be an easy thing to add to the "won't change" PEP ...
> >
> > The latter, please.
> >
> > We apparently need more guidance for when it's appropriate to turn
> > something into an attribute. I don't have time to write that up right
> > now; if someone else can give it a shot, I'd appreciate it; or I'll
> > come back to this topic later.
>
> Just a data point.  In our API, we've been converting all of our no-arg
> methods to attributes/properties, each of which may or may not be
> writable of course.  It's simplified our Python code enormously, and
> made it much more readable.  Certainly "foo.get_something()" and
> "foo.set_something(thing)" are much better spelled "foo.something" and
> "foo.something = thing".
>

I would amend that to anything that does not cause mutation when
calling (this does not include creating new objects, though) or mutate
other things when assigning (thanks to properties that is possible). 
Basically that is my personal rule of thumb for when I code.

-Brett

> As a general principle, we've been making this conversion for any
> getters and setters that take no arguments, both for pure-Python types
> and C-implemented types.  While we were at it, we changed everything
> that returns a newly formed concrete sequence to returning an iterator
> (which broke code that was doing random access into the sequence, but
> that's another story).
>
> I don't know whether Python 3000 wants to encourage that style, but it
> works well for us.
>
> -Barry
>
>
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.2.2 (GNU/Linux)
>
> iQCVAwUARDLLiXEjvBPtnXfVAQJwnwP/fl3UBFDXUMS+DeNMLbQYJwmeJG09v8oJ
> ER1yq35Zqs2R3YN9y1ufYiMSXqy/F5szlDs4VnjpQL5t1B7DYmAj+QqW0y6i2CZd
> Yd6vzehlEWdMqHc2cmgrXlmPr/AQWodfghrbAemu+kRTjQ4nkrv9jc1Zvfw3YxRg
> v2gXHyn66aU=
> =MmiI
> -----END PGP SIGNATURE-----
>
>
> _______________________________________________
> Python-3000 mailing list
> Python-3000 at python.org
> http://mail.python.org/mailman/listinfo/python-3000
> Unsubscribe: http://mail.python.org/mailman/options/python-3000/brett%40python.org
>
>
>


More information about the Python-3000 mailing list