slice notation as values?
Duncan Booth
duncan.booth at invalid.invalid
Sun Dec 11 05:49:31 EST 2005
Brian Beck wrote:
> Antoon Pardon wrote:
>> Will it ever be possible to write things like:
>>
>> a = 4:9
>
> I made a silly recipe to do something like this a while ago, not that
> I'd recommend using it. But I also think it wouldn't be too far-fetched
> to allow slice creation using a syntax like the above...
>
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/415500
>
Another possibility would be to make the slice type itself sliceable, then
you could write things like:
>>> a = slice[4:9]
>>> a
slice(4, 9, None)
Sample implementation:
>>> class MetaSlice(object):
def __getitem__(cls, item):
return item
def __init__(self, *args, **kw):
return super(MetaSlice,self).__init__(self, *args, **kw)
>>> class Slice(slice):
__metaclass__=MetaSlice
>>> Slice[2:3]
slice(2, 3, None)
>>> Slice[:3]
slice(None, 3, None)
>>> Slice[:3:-1]
slice(None, 3, -1)
More information about the Python-list
mailing list