简体   繁体   English

Python 类引用的命名约定是什么

[英]What is the naming convention for Python class references

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.我更喜欢 ReferenceToClass,每个刚接触代码的人都知道它是一个类而不是一个实例。 But as poplitea wrote , literature reference would be great.但正如poplitea 所写,参考文献会很棒。

On module level the second:在模块级别第二个:

ReferenceToClass = MyClass

As a function argument, the first:作为函数参数,第一个:

reference_to_class = MyClass

tl;dr : for global/public names use AllCaps like XORcist said : tl;dr :对于全局/公共名称,请使用像AllCaps所说的 AllCaps:

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."class"这个词实际上在你的类名中时。 For classobj , see also class_ and klass .对于classobj ,另请参见class_klass


Analysis/Motivation (long version)分析/动机(长版)

No thorough reading, but at a glance PEP 8 doesn'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 binding in 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.由于变量名可能只是 yet-another name binding in python,在我看来,将该名称与定义块绑定还是稍后使用=等号绑定到某个对象并不重要。

For this I agree with XORcist in 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_underscores should apply, right?但是,当涉及到参数和变量名称时,应该使用lowercase_underscores ,对吧? 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:就此而言,我建议在全小写的类引用变量名后加上“类”后缀,如下所示:

class Logger(object):
    pass

def function_expecting_class_reference(logger_class):
    pass

I renamed your example class MyClass to Logger because in real scenarios only a few class name contains the string "class" .我将您的示例类MyClass重命名为Logger因为在实际场景中只有少数类名包含字符串"class" However in that latter case I propose to avoid the ambiguity with descriptive naming yet again.然而,在后一种情况下,我建议再次避免描述性命名的歧义。 For example, you may use a classobj suffix:例如,您可以使用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 .我倾向于采用的另一种选择是使用后缀klass ,例如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.似乎不是每个人都得到后者,但无论如何我还没有测试他们是否会让前者变得更好。

I treat it the same as an instance variable, which PEP8 defines as using lowercase_underscore_style.我将其视为实例变量,PEP8 将其定义为使用 lowercase_underscore_style。 (lowercase, with words separated by underscores as necessary to improve readability.) (小写,必要时用下划线分隔单词以提高可读性。)

http://www.python.org/dev/peps/pep-0008/#id34 http://www.python.org/dev/peps/pep-0008/#id34

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM