Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么太长的变量名会导致';语法错误:无效语法';?_Python_Python 2.7 - Fatal编程技术网

Python 为什么太长的变量名会导致';语法错误:无效语法';?

Python 为什么太长的变量名会导致';语法错误:无效语法';?,python,python-2.7,Python,Python 2.7,下一行代码导致语法错误:无效语法 #coding=utf-8 result_3_logspace_mean_proportion_сorrect_answers = Exception('3_logspace_mean_proportion_сorrect_answers').get_result(result_1_main) 而第二行代码不是: #coding=utf-8 result_3 = Exception('3_logspace_mean_proportion_сorrect

下一行代码导致语法错误:无效语法

#coding=utf-8    
result_3_logspace_mean_proportion_сorrect_answers = Exception('3_logspace_mean_proportion_сorrect_answers').get_result(result_1_main)
而第二行代码不是:

#coding=utf-8
result_3 = Exception('3_logspace_mean_proportion_сorrect_answers').get_result(result_1_main)

如何处理这个问题?我非常想要第一个变量名。

正确的
开头的字母不是
c
,它是西里尔字母
c
,Python 2在源代码中默认只接受ASCII。

第一个变量名包含一个非ASCII字符:“正确”中的第一个“c”是一个非ASCII字符。如果将字符串解码为ascii,则可以看到:

#Python3
>>> 'result_3_logspace_mean_proportion_сorrect_answers'.encode()
'result_3_logspace_mean_proportion_\xd1\x81orrect_answers'

#Python2
>>> u'result_3_logspace_mean_proportion_сorrect_answers'.encode('utf8')
'result_3_logspace_mean_proportion_\xd1\x81orrect_answers'

将其替换为正常的“c”可以解决此问题。

问题可能实际上在上面的行中。也许括号没有闭合?这个变量名没有问题。我在Python 2中得到了一个第一行的SyntaxError,但在Python中没有3@ThierryLathuille经过验证,我也有同样的行为。Python2不允许在变量名中使用非标准的
c
这样的任意Unicode。这就是为什么会突出显示特定的角色。