Hi There,

On Fri, Jun 03, 2011 at 06:44:35PM -0700, Nils Bruin wrote:
> On Jun 3, 4:47 pm, Jason Grout <jason-s...@creativetrax.com> wrote:
> > I don't know that it will make much difference to look into exactly why
> > the M.list() method doesn't exist anymore, given that list(M) seems to
> > be equivalent.  However, looking into this might uncover a place where a
> > deprecation warning should have been placed to warn users who used M.list().
> 
> The mechanisms are different. For list(M), one only needs that M has
> an iter() method, whereas M.list() is simply a method on M. By default
> I'd say that list(M) would be the more pythonic and hence preferred
> method. However, the referee of #10470 has a different opinion. In
> 
> http://trac.sagemath.org/sage_trac/attachment/ticket/10470/trac_10470-referee-doc.patch
> 
> It is claimed that M.list() is preferable for for sage objects.
> Possible reasons for this are:
>  - It allows raising an error rather than infinite iteration if M is
> iterable but not finite.
>  - It is conceivable that if one really needs M.list(), it can be more
> efficient to produce this in one go than with an iterator.
> On the other hand, objects implementing the iterator protocol are more
> versatile so in general they should probably do that before providing
> a .list() method.

Here is a third reason (actually not very different from the previous one):
sometime computing __iter__ is very time consuming, for example because of a
recursive iterator. Using .list() allows to cache it and speedup things. see

    #11118 "Add a cache for .list() method in FiniteEnumeratedSet"

> If we should indeed prefer M.list() over list(M), perhaps we should
> provide a list method on SageObject that just returns list(self)? That
> way objects are OK if they just provide .iter(), but if producing a
> list efficiently is desirable for that object, the .list method can be
> overridden.

Please don't ! At least not in SageObject. Rationale: most of object who have
a .list() method are actually Parent. In several categories, a proper .list()
method is defined. Since category are after concrete class in the mro, adding
.list() to SageObject will completely prevent defining it in categories. Here
is a proposition of solution: Put you object in the correct category which
define .list(). This would probably be InfiniteSets() though it currently
doesn't exists. In combinatorics, we use a lot InfiniteEnumeratedSet() which
is according to the doc
"""
    The category of infinite enumerated sets

    An infinite enumerated sets is a countable set together with a
    canonical enumeration of its elements.
"""
If this is indeed your need, we can lift .list() to a category
InfiniteSets(). What do you think ?

Florent

Note: in #10963 Nicolas is writing a framework to automatically create
"category with an adjective" such as Sets().Infinite()

-- 
To post to this group, send an email to sage-devel@googlegroups.com
To unsubscribe from this group, send an email to 
sage-devel+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

Reply via email to