Range Operation pre-PEP

Andrew Dalke dalke at acm.org
Wed May 9 14:20:56 EDT 2001


Carlos Ribeiro wrote:
>The a..b syntax is a nice way to specify arbitrary set
>constants, such as in:
>
>if some_number in [1..3,7..9]:
>    do_something()

Something like that can already be done without introducing
new syntax, as in

>>> import types
>>> class Set:
...  def __getitem__(self, terms):
...   data = []
...   for term in terms:
...    if type(term) == types.SliceType:
...     for x in range(term.start, term.stop):
...      data.append(x)
...    else:
...     data.append(term)
...   return data
...
>>>
>>> Set = Set()
>>> Set[1:3, 7:9]
[1, 2, 7, 8]
>>> Set[1, 4:10]
[1, 4, 5, 6, 7, 8, 9]
>>>

                    Andrew
                    dalke at acm.org






More information about the Python-list mailing list