Python 类引用的命名约定是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13765980/
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
What is the naming convention for Python class references
提问by Peter Hudec
What is the naming convention for a variable referencing a class in Python?
在 Python 中引用类的变量的命名约定是什么?
class MyClass(object):
pass
# which one is correct?
reference_to_class = MyClass
# or
ReferenceToClass = MyClass
Here is another example that resembles my situation:
这是另一个类似于我的情况的示例:
# cars.py
class Car(object):
pass
class Sedan(Car):
pass
class Coupe(Car):
pass
class StatonWagon(Car):
pass
class Van(Car):
pass
def get_car_class(slug, config):
return config.get(slug)
# config.py
CONFIG = {
'ford-mustang': Coupe,
'buick-riviera': Coupe,
'chevrolet-caprice': Sedan,
'chevy-wan' Van:
'ford-econoline': Van
}
# main.py
from config.py import CONFIG
from cars import get_car_class
MyCarClass = get_car_class('buick-riviera')
my_car = MyCarClass()
I would prefer ReferenceToClass, that everybody new to the code knows it's a class and not an instance. But as @poplitea wrote, literature reference would be great.
我更喜欢 ReferenceToClass,因为每个不熟悉代码的人都知道它是一个类而不是一个实例。但正如@poplitea 所写,参考文献会很棒。
采纳答案by XORcist
On module level the second:
在模块级别上,第二个:
ReferenceToClass = MyClass
ReferenceToClass = MyClass
As a function argument, the first:
作为函数参数,第一个:
reference_to_class = MyClass
reference_to_class = MyClass
回答by Corey Goldberg
I treat it the same as an instance variable, which PEP8 defines as using lowercase_underscore_style. (lowercase, with words separated by underscores as necessary to improve readability.)
我将它视为一个实例变量,PEP8 将其定义为使用lowercase_underscore_style。(小写,根据需要用下划线分隔单词以提高可读性。)
回答by n611x007
tl;dr: for global/public names use AllCapslike XORcist said:
tl;dr:对于全局/公共名称使用,AllCaps如 XORcist 所说:
class Logger:
pass
AliasLogger = Logger
For function parameters and function locals, make it clear that you are dealing with the class object with a descriptive name like this:
对于函数参数和函数局部变量,请清楚说明您正在处理具有如下描述性名称的类对象:
def some_func(logger_class):
pass
or something along the lines
或类似的东西
def some_func(my_class_classobj):
pass
when the word "class"is actually in your classname. For classobj, see also class_and klass.
当这个词"class"实际上在你的类名中时。对于classobj,另见class_和klass。
Analysis/Motivation(long version)
分析/动机(长版)
No thorough reading, but at a glance PEP 8doesn't seem to be explicit on this (neither google's python style guide for that matter).
没有彻底阅读,但乍一看PEP 8似乎并没有明确说明这一点(谷歌的 python 风格指南都没有)。
Since a variable name is probably just yet-another name bindingin python, in my opinion it doesn't really matter whether you bind that name with the definition block or later with the =equal sign to some object.
由于变量名可能只是python中的另一个名称绑定,在我看来,是将该名称与定义块绑定还是稍后用=等号绑定到某个对象并不重要。
For this I agree with XORcistin that module level "alias" references should adhere to your class naming standard, probably AllCaps:
为此,我同意XORcist的模块级“别名”引用应遵守您的类命名标准,可能是 AllCaps:
class MyClass(object):
pass
# good
ReferenceToClass = MyClass
However when it comes to parameter and variable names, supposedly lowercase_underscoresshould apply, right? I'm unhappy with only that, since it will push you into the instance vs class reference ambiguity. There is the potential that an all-lowercase name may be an attempt to hint the object being an instance. For that matter, I recommend postfixing your all-lowercase, class-referencing variable names with the "class" suffix, like this:
但是,当涉及到参数和变量名称时,应该lowercase_underscores适用,对吗?我对此并不满意,因为它会将您推入实例与类引用的歧义。全小写的名称有可能试图暗示对象是一个实例。就此而言,我建议使用“class”后缀对全小写的类引用变量名进行后缀,如下所示:
class Logger(object):
pass
def function_expecting_class_reference(logger_class):
pass
I renamed your example class MyClassto Loggerbecause in real scenarios only a few class name contains the string "class". However in that latter case I propose to avoid the ambiguity with descriptive naming yet again. For example, you may use a classobjsuffix:
我将您的示例类重命名为MyClass,Logger因为在实际场景中只有少数类名包含字符串"class". 然而,在后一种情况下,我建议再次避免描述性命名的歧义。例如,您可以使用classobj后缀:
class MyClass(object):
pass
def function_expecting_class_reference(another_param, my_class_classobj):
ReferenceToClass = MyClass
Another alternative I tend to take is to use the suffix klass, like my_class_klass. Not everyone seems to get the latter, but anyway I'm yet to test whether they would get the former any better.
我倾向于采用的另一种选择是使用后缀klass,例如my_class_klass. 不是每个人似乎都得到后者,但无论如何我还没有测试他们是否会让前者更好。

