On 05/02/2016 06:07 AM, Erik Bray wrote:
> On Mon, May 2, 2016 at 10:35 AM, Jeroen Demeyer <jdem...@cage.ugent.be> wrote:
>> My vote:
>>
>>> [X] Phase out properties which perform any non-trivial computation
>>
>>
>> In certain cases, properties might be useful (but it could very well be that
>> there are 0 such cases in Sage).
>
> I generally feel that properties *should* be used in general for
> invariants of some object, regardless of how it's computed in the
> first place. I see the point about not using them for "non-trivial"
> computations but I also find the lack of a clear definition of
> "non-trivial" troubling.
>
Properties, in any programming language, are syntactic sugar over
getter/setter methods on private member variables. In a language like
C#, they're useful and you have a sensible rule for when to use them:
use properties to get/set private member variables, and methods for
everything else. So basically, properties do no computation at all.
In python, all member variables are public, so the concept of a property
is a bit redundant. It's hard to come up with a "when to use properties"
rule in python, because the only rule that makes sense doesn't apply.
Anywhere you could use a property, you can use a "public" member
variable instead (if you can't, then you wanted a method to begin with).
The one useful feature of @property is that it lets you document your
member variables. If I have a class with a "public" nrows member
variable, then I can't document it so that when a user runs foo.nrows?,
it tells him what that variable is supposed to do. By creating a
property (which is only syntactic sugar over a getter method), I gain
the ability to add a docstring on what would otherwise be a variable.
So, the rule I would put forth is: use @property to document public
member variables, and methods for anything else.
--
You received this message because you are subscribed to the Google Groups "sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+...@googlegroups.com.
To post to this group, send email to sage-...@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.
Overall, I think that the problem with documentation is worse than the gain with tab-completion, but it would be so nice if we could do something like:sage: n.transpose().[tab]
I don't think it's just "syntactic sugar". If anything it's setter
and *especially* getter methods that are backwards, but unfortunately
necessary in languages like Java that don't have a natural way to
interpose in attribute access.
(As an aside, I would tend to agree that matrix.T *should* be a method. But I also see M.T without parens as syntactic sugar itself, and a rough approximation of M^T, with which people are familiar. Perhaps exceptions like these are good uses for a lazily-evaluated proxy object, but I haven't thought through all the possible implications of that for something as complex as a matrix (most of the time I've only used these for built-in types likes int and str).
<quote>
A few minutes ago we merged a PR[1] adding jedi[2] integration to IPython, this make the IPython completer a bit smarter, as it now knows about situation like: In[1]: ('je'+'di').upper().<tab> Where it will infer that you are actually calling a method on a string, which before was requiring setting `IPCompleter.greedy` to `true` which has the drawback of evaluating your code with its side effects.
</quote>
Thanks,
Jason
FYI, a relevant post just appeared on the ipython dev list: https://mail.scipy.org/pipermail/ipython-dev/2016-May/017099.html