Are slice objects mutable in function declaration?

358 views Asked by At

After learning about how default arguments work in python, I went over all my code to look for potential bugs occuring when using mutable sequences.

Now I have a function whose signature is:

def get_measurements(self, shape = slice(None, None, None), 
                     size = slice(None, None, None), 
                     height = slice(None, None, None), 
                     pressure = slice(None, None, None),  
                     LE = slice(None, None, None),                          
                     fname = None)

And I'm wondering now, are slice objects mutable? And will this cause a problem in above case with default values?

1

There are 1 answers

3
reynoldsnlp On

slice objects are not mutable.

>>> s = slice(None)
>>> s
slice(None, None, None)
>>> s.start = 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: readonly attribute
>>> s.stop = 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: readonly attribute
>>> s.step = 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: readonly attribute