Enno Gröper
Enno Gröper

Reputation: 4469

How to use single colon when using variable for slicing?

I want to use a variable for storing a slicing expression. This is what I try:

    if variable is not None:
        var = variable
    elif self.__filter is not None:
        var = self.__filter
    else:
        # this doesn't work, : alone understandably gives a syntax error
        var = ':'

var is then used here:

return stuff[var]

I want to do this to get a better structure in my code. The alternative would be to return this expressions depending on case:

return stuff[variable]
return stuff[self.__filter]
return stuff[:]

Exception raised: ValueError: invalid literal for int() with base 10

Is there a way to assign the colon to var so it can be used in the slicing expression (escaping or similar stuff) or do I have to use var = slice(0, self.getNumberOfVariables()) instead?

P.S.: I did search before asking, but didn't find anything like this. Sorry if I missed a duplicate.

Upvotes: 6

Views: 2975

Answers (3)

The Salt
The Salt

Reputation: 476

This is an old post but it just popped up as a top result on Google, so thought I'd answer it to help any others.

The colon : in a slice is just a separator between [start:stop:step]. What is often not explicitly written is None when one of start, stop or step are left to their default values.

This is easier to see in examples:

# Slice with all parameters specified
# start:1, stop:4, step:1    
seq[1:4:1]

# Slice with default step value (default=1)
# start:1, stop:4, step:1
# All the following are equivalent    
seq[1:4:None]
seq[1:4:]
seq[1:4]

# Slice with default stop value (default = len(seq))
# start:1, stop:len(seq), step:1
# All the following are equivalent    
seq[1:None:1]
seq[1::1]

# Slice with default step and stop value
# start:1, stop:len(seq), step:1
# All the following are equivalent    
seq[1:None:None]
seq[1::]
seq[1:]

# Slice with default start value
# start:1, stop:4, step:1
# All the following are equivalent    
seq[None:4:1]
seq[:4:1]

# Slice with all default values
# start:1, stop:len(seq), step:1
# All the following are equivalent    
seq[None:None:None]
seq[::]
seq[:]

So to answer your question, to use the : in a slice variable doesn't make sense, since you're asking to use a separator. Though it is confusing since you don't have to explicitly state None when creating a slice directly, so this makes the : look like an operator.

TLDR; when using the slice type, you must explicitly state None. So seq[slice(None, None, None)] is equivalent to seq[::].

Upvotes: 5

Michael J. Barber
Michael J. Barber

Reputation: 25042

It is not possible to use the colon directly. It is a literal notation for an operation; Python does not generally allow this sort of punctuation to be used directly, instead requiring use of a function (cf. arithmetic operators and the corresponding functions in the operator module).

The function to use is the builtin function slice, which return an explicit representation of a slice. Use slice(None) to include everything.

An example:

>>> s = slice(None)
>>> x = range(10)
>>> x[s]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Upvotes: 11

Enno Gröper
Enno Gröper

Reputation: 4469

The expression stuff[var] immutably fixes the possibilities to something, that can be expressed using a single variable. You have to think about how stuff gets interpreted. First the code gets parsed. At this time gets fixed which syntax is used. start:stop:step is an expression, the compiler accepts as a slice. A special case of this is :. Other possibilities are scalars, lists and slice objects. Single objects.

Using var in the getter, I can only set var to a list, a scalar or a slice object. When trying to assign colon syntax stuff to var, I just get a string literal and string literals don't get parsed by the getter/setter methods. you simply can't change the syntax by assigning some value to a variable.

Upvotes: -1

Related Questions