python - 切片对象在函数声明中是否可变?

标签 python default-value mutable

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

现在我有一个函数,其签名是:

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)

我现在想知道,切片对象是可变的吗?这会在上述情况下使用默认值导致问题吗?

最佳答案

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

关于python - 切片对象在函数声明中是否可变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30729055/

相关文章:

python - 如何在单行中插入 SQL 表中的所有嵌套字典值

python - 每次部署后都被迫重新安装 Python 包(Flask + Azure 应用服务)

Django:数据库中的默认值,而不仅仅是 ORM

.net - 为什么在using语句内的闭包内捕获可变结构变量会改变其局部行为?

java - 安卓位图操作

python - 如何计算没有空格的字符串中的字母数?

python - 方程 latex 代码的余弦相似度

oracle - 可选的输出游标作为参数

c++ - 成员属性作为方法的默认值

multithreading - Clojure 可变存储类型