slice notation as values?

Antoon Pardon apardon at forel.vub.ac.be
Sat Dec 10 11:01:17 EST 2005


On 2005-12-10, Steven Bethard <steven.bethard at gmail.com> wrote:
> Antoon Pardon wrote:
>> So lets agree that tree['a':'b'] would produce a subtree. Then
>> I still would prefer the possibility to do something like:
>> 
>>   for key in tree.iterkeys('a':'b')
>> 
>> Instead of having to write
>> 
>>   for key in tree['a':'b'].iterkeys()
>> 
>> Sure I can now do it like this:
>> 
>>   for key in tree.iterkeys('a','b')
>> 
>> But the way default arguments work, prevents you from having
>> this work in an analague way as a slice.
>
> How so?  Can't you just pass the *args to the slice contstructor?  E.g.::
>
>      def iterkeys(self, *args):
>          keyslice = slice(*args)
>          ...
>
> Then you can use the slice object just as you would have otherwise.

This doesn't work for a number of reasons,

1) 

>>> slice()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  TypeError: slice expected at least 1 arguments, got 0


2) It doens't give a clear way to indicate the following
   kind of slice: tree.iterkeys('a':). Because of the
   follwing:

>>> slice('a')
slice(None, 'a', None)

   which would be equivallent to tree.iterkeys(:'a')

-- 
Antoon Pardon



More information about the Python-list mailing list