#20743: Parent.list() should return an immutable tuple
-------------------------------------+------------------------
Reporter: jsrn | Owner:
Type: defect | Status: new
Priority: major | Milestone: sage-7.3
Component: categories | Resolution:
Keywords: parent, immutable | Merged in:
Authors: | Reviewers:
Report Upstream: N/A | Work issues:
Branch: | Commit:
Dependencies: | Stopgaps:
-------------------------------------+------------------------
Comment (by nbruin):
Replying to [comment:2 kedlaya]:
> -1 to returning a tuple. `foo.list()` should return the same as
`list(foo)`, which is always a list, and `foo.tuple()` should return the
same as `tuple(foo)`, which is always a tuple. And for that matter,
`foo.str()` returns the same as `str(foo)` which is always a string, et
cetera.
This sounds quite reasonable, but the implementation details are quite
different: `list(...)` and `tuple(...)` simply investigate the argument
for being iterable and if it is, exhaust the iterator and put the results
in a tuple/list. On the other hand, `foo.list()` and/or `foo.tuple()` ask
the object to return a list and/or tuple. It can do this much more
efficiently when it's cached. The "safe" option is to return a copy, but
that just means incurring the cost of making a copy immediately.
The story for `str` is different because that investigates if the object
implements the `__str__()` protocol, which is a much more straightforward
translation (and strings are immutable), so I'll leave that out of
consideration here.
I think there is definitely a place for giving efficient access to the
constituents of an object in the form of an indexable structure (tuple or
list). It seems that it's a bug trap to do this in the form of a mutable
data structure (probably because the similarity to `list(foo)`, where a
fresh list gets constructed automatically), so perhaps a tuple is a better
fit.
The method for this has traditionally been called `foo.list()` in sage.
Changing this to `foo.tuple()` is perhaps a little heavy-handed. I think
no-one would be looking for this method unless they already now it's what
you should look for. Are there other names that are more evocative?
In python3, dictionaries have readonly "views" for their keys,values, and
items (although they don't admit indexing). Perhaps the thing we need is
something that provides an "indexable view" of the contents of an object.
Tuples would work. Lists would too, but are mutable.
I'm not sure how important it is that we specify the exact type that needs
to be returned. In pure python this shouldn't be important at all, but in
cython you get much more efficient code if something is known to be a
tuple or a list.
--
Ticket URL: <http://trac.sagemath.org/ticket/20743#comment:3>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica,
and MATLAB
--
You received this message because you are subscribed to the Google Groups
"sage-trac" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sage-trac.
For more options, visit https://groups.google.com/d/optout.