| 993 | | """ |
| 994 | | Return a list of all elements in this object, if possible (the |
| 995 | | object must define an iterator). |
| | 993 | r""" |
| | 994 | Return a list of the elements of ``self``. |
| | 995 | |
| | 996 | OUTPUT: |
| | 997 | |
| | 998 | A list of all the elements produced by the iterator |
| | 999 | defined for the object. The result is cached. An |
| | 1000 | infinite set may define an iterator, allowing one |
| | 1001 | to search through the elements, but a request by |
| | 1002 | this method for the entire list should fail. |
| | 1003 | |
| | 1004 | EXAMPLES:: |
| | 1005 | |
| | 1006 | sage: R = Integers(11) |
| | 1007 | sage: R.list() # indirect doctest |
| | 1008 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
| | 1009 | |
| | 1010 | sage: ZZ.list() |
| | 1011 | Traceback (most recent call last): |
| | 1012 | ... |
| | 1013 | ValueError: since it is infinite, cannot list Integer Ring |
| | 1014 | |
| | 1015 | This is the motivation for Trac ticket #10470. :: |
| | 1016 | |
| | 1017 | sage: (QQ^2).list() |
| | 1018 | Traceback (most recent call last): |
| | 1019 | ... |
| | 1020 | ValueError: since it is infinite, cannot list Vector space of dimension 2 over Rational Field |
| | 1021 | |
| | 1022 | TESTS: |
| | 1023 | |
| | 1024 | The following tests the caching by adjusting the cached version. :: |
| | 1025 | |
| | 1026 | sage: R = Integers(3) |
| | 1027 | sage: R.list() |
| | 1028 | [0, 1, 2] |
| | 1029 | sage: R._list[0] = 'junk' |
| | 1030 | sage: R.list() |
| | 1031 | ['junk', 1, 2] |
| | 1032 | |
| | 1033 | Some objects do not know if they are finite or not, and so a request |
| | 1034 | for the entire list of an infinite set could run without stopping. |
| | 1035 | Asking for ``F.list()`` below will not finish without an interruption |
| | 1036 | like pressing Ctrl-C. Try it. :: |
| | 1037 | |
| | 1038 | sage: R.<t> = QQ[] |
| | 1039 | sage: F = FractionField(R) |
| | 1040 | sage: F.is_finite() |
| | 1041 | Traceback (most recent call last): |
| | 1042 | ... |
| | 1043 | NotImplementedError |