slice对象在函数声明中是否可变?

时间:2015-06-09 10:08:28

标签: python default-value mutable

在了解了python中的default arguments work之后,我查看了所有代码,以查找使用可变序列时可能发生的错误。

现在我有一个签名为:

的函数
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)

我现在想知道,切片对象是否可变?这会导致上述情况下出现问题吗?

1 个答案:

答案 0 :(得分:1)

slice对象是不可变的。

>>> 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