gpt4 book ai didi

没有缩进的 Python def

转载 作者:行者123 更新时间:2023-11-28 20:05:47 25 4
gpt4 key购买 nike

在我的源文件中多次出现以下格式(注意 def 行后没有缩进):

def sendSystemParms2(self, destAddress, subid, state):

raw = sysparms_format2.build(Container(
length = 0x0D,
command = MT_APP_MSG_CMD0))

def method2(parm2):
print parm2

它正在运行。我很困惑。有什么提示吗?由于是新图片且没有足够的声誉,我无法上传图片,但我可以出示证据。

最佳答案

您有一个混合使用制表符和空格的文件。

Python 将制表符扩展为 八个空格,但您正在使用制表位大小为 四个空格 的编辑器中查看文件。

函数体使用制表符进行缩进,但 def 行改为使用 4 个空格。就 Python 而言,方法体已正确缩进。

如果我将我的文本编辑器设置为使用大小为 8 个空格的制表符,然后选择文本让编辑器突出显示制表符,我会看到:

source code with indentation highlighted

线条表示制表符。

这是您根本不应该使用制表符进行缩进的原因之一。Python style guide建议您仅使用空格进行缩进。在 Python 3 中,像这样混合制表符和空格是语法错误。

您也可以通过使用 -tt command line switch 运行 Python 告诉 Python 2 为这种制表符和空格的混合引发 TabError :

-t
Issue a warning when a source file mixes tabs and spaces for indentation in a way that makes it depend on the worth of a tab expressed in spaces. Issue an error when the option is given twice (-tt).

关于没有缩进的 Python def,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27389475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com