函数/变量合法语法

2024-10-03 06:27:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我理解我们遵循自己的命名惯例的想法,但这完全是这样吗?例如,函数/变量名不能以数字开头,但可以包含“特殊”字符,对吗?但是:

@methodtrace(utils.logger)

给我提问题。@.是否显著?我读过What is the naming convention in Python for variable and function names?


Tags: the函数inforis数字utilslogger
1条回答
网友
1楼 · 发布于 2024-10-03 06:27:57

这不是编码样式(Python的样式是在PEP-8中定义的),而是语法的问题。你发布的不是一个名字。意思是:

@methodtrace(utils.logger)
^ decorate the following function
 ^ with the result of calling the function methodtrace
             ^ with a single argument, the logger attribute of utils

有效的Python标识符是在the documentation中定义的,它必须以字母(大写或小写)开头,然后包含零个或多个字母或数字。@是修饰函数的语法糖(参见What does the "at" (@) symbol do in Python?),括号是调用函数/方法的语法,.用于访问对象的属性。你知道吗

相关问题 更多 >