在 Pillow (PIL) 模块中使用 thumbnail()
方法时,必须遵守以下约定,其中使用了两组(嵌套)括号:
pil_image.thumbnail((128,128))
如果只使用一组,Python 会产生以下错误:
TypeError: 'int' 对象不可订阅
最里面的括号有正式名称吗?
PS:以下也有效:
box = 128, 128 # No parentheses needed.
pil_image.thumbnail(box)
另外:Python 解析器的一部分是否存在缺陷,无法弄清楚这里的意图?
最佳答案
您正在传递 tuple到 thumbnail()
方法。区别如下:
thumbnail((128,128)) # single tuple argument: (128,128)
thumbnail(128,128) # two integer arguments: 128 and 128
您的 box
变量也是一个元组,这就是它同样有效的原因:
>>> box = 128, 128
>>> box
(128, 128)
>>>
>>> type(box)
<type 'tuple'>
关于python - 最里面的括号叫什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21663576/