[AstroPy] Coding Guidelines draft (comments encouraged)
Perry Greenfield
perry at stsci.edu
Mon Jul 18 07:44:03 EDT 2011
On Jul 18, 2011, at 7:33 AM, Erik Tollerud wrote:
>
>> Realistically, I think people are likely to want to use the
>> get_color()/set_color() approach because it is more natural. It also
>> makes it more clear what is happening. I know that hard core OO guys
>> think properties are cool, but I've never seen the attraction myself.
>
> I don't find get_color/set_color to be natural at all - the
> property/attribute access seems much more so to me... although I don't
> think it's too hard to adapt from one style to the other (in either
> direction) for this case. The advantage of properties in this context
> is that they encourage a natural flow between simple attribute access
> (which should be used when nothing else is needed) to get/set methods
> (properties look like attribute access). On top of that, "star.color
> = 0.4" is more readable, and is a bit faster to type than
> "star.set_color(.4)". Properties in the context given here are also
> suggested by PEP8 (mostly for these reasons).
I agree with Erik on this point. If something looks like an attribute
(even if it is computed, or needs to call a function when set), then
the attribute interface is the most natural. As Erik also mentions, it
allows starting with a simple attribute and not requiring a change to
the interface to use a method (why other languages think that all
attributes always should use setters and getters when they don't have
a properties mechanism).
Perry
More information about the AstroPy
mailing list