Python Pylint 无效常量名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25184097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:50:29  来源:igfitidea点击:

Pylint invalid constant name

pythoncoding-stylepylint

提问by gcamargo

I'm receiving a Pylint error regarding my constant: MIN_SOIL_PARTICLE_DENS(invalid name). Any ideas why this constant is wrong? Here's my full function:

我收到关于我的常量的 Pylint 错误:(MIN_SOIL_PARTICLE_DENS无效名称)。任何想法为什么这个常数是错误的?这是我的完整功能:

def bulk_density(clay, sand, organic_matter):
    MIN_SOIL_PARTICLE_DENS = 2.65
    x1 = (0.078 + 0.278 * sand + 0.034 * clay + 0.022 * organic_matter - 0.018
          * sand * organic_matter - 0.027 * clay * organic_matter - 0.584 * sand
          * clay)
    x2 = -0.107 + 1.636 * x1
    field_capacity = vol_water_content_33_j_kg(clay, sand, organic_matter)#m3/m3
    sat_water_content = 0.043 + field_capacity + x2 - 0.097 * sand
    return (1 - sat_water_content) * MIN_SOIL_PARTICLE_DENS

采纳答案by Reiner Gerecke

When checking names, Pylint differentiates between constants, variables, classes etc. Any name that is not inside a function/class will be considered a constant, anything else is a variable.

在检查名称时,Pylint 会区分常量、变量、类等。任何不在函数/类中的名称都将被视为常量,其他任何名称都是变量。

See http://docs.pylint.org/features.html#basic-checker

请参阅http://docs.pylint.org/features.html#basic-checker

variable-rgx:
[a-z_][a-z0-9_]{2,30}$

const-rgx:
(([A-Z_][A-Z0-9_]*)|(__.*__))$

变量-rgx:
[a-z_][a-z0-9_]{2,30}$

常量-rgx:
(([A-Z_][A-Z0-9_]*)|(__.*__))$

Because you're in a function, MIN_SOIL_PARTICLE_DENSis (according to pylint) supposed to be a variable, pylint however treats it as a constant and therefore complains.

因为您在一个函数中,MIN_SOIL_PARTICLE_DENS所以(根据 pylint)应该是一个变量,但是 pylint 将其视为常量,因此会抱怨。

This means you can't have any uppercase names inside functions without pylint complaining.

这意味着您不能在没有 pylint 抱怨的情况下在函数中使用任何大写名称。



If you ask me, using uppercase inside functions is fine; not all constants are necessarily defined globally.

如果你问我,在函数内部使用大写是可以的;并非所有常量都必须全局定义。

回答by Vishvajit Pathak

Few simple rules :

几个简单的规则:

  1. Constants should be defined with UPPER_CASEletters only and should be defined at the module level
  2. Class names should be defined with CamelCaseletters
  3. Variables should be defined at lower_caseand should be defined inside function, classes etc.
  1. 常量应该UPPER_CASE只用字母定义,并且应该在模块级别定义
  2. 类名应该用CamelCase字母定义
  3. 变量应该lower_case在函数、类等中定义并且应该在内部定义。

Now lets talk about your case,

现在让我们谈谈你的情况,

MIN_SOIL_PARTICLE_DENSis defined inside a function and should have lower letters only. Thus instead of considering MIN_SOIL_PARTICLE_DENSas a constant, pylint considers it as a variable here and hence the pylint error.

MIN_SOIL_PARTICLE_DENS在函数内部定义并且应该只有小写字母。因此MIN_SOIL_PARTICLE_DENS,pylint不是将其视为常量,而是将其视为变量,因此是 pylint 错误。

Pylint Tutorial

Pylint 教程

回答by ComFreek

I found this behavior annoying, but there's a way to configure pylint to avoid this!

我发现这种行为很烦人,但是有一种方法可以配置 pylint 来避免这种情况!

Merge the following ini-style declaration into your .pylintrcfile:

将以下 ini 样式声明合并到您的.pylintrc文件中:

[BASIC]
variable-rgx=((([a-z_][a-z0-9_]{2,})|(_[a-z0-9_]*)|(__[a-z][a-z0-9_]+__))$)|([A-Z_][A-Z0-9_]+$)

I built this regex by taking

我通过采取建立这个正则表达式

and joining them by |and some parentheses.

并通过|一些括号加入它们。

Theoretically, you could also just take .*, but this would allow even invalid names like mixed_CASE.

从理论上讲,您也可以只使用.*,但这甚至会允许无效名称,例如mixed_CASE.