What is the naming convention for Python class references

前端 未结 3 551
忘掉有多难
忘掉有多难 2021-02-03 19:22

What is the naming convention for a variable referencing a class in Python?

class MyClass(object):
    pass

# which one is correct?
reference_to_class = MyClass
         


        
相关标签:
3条回答
  • 2021-02-03 20:00

    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.)

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

    0 讨论(0)
  • 2021-02-03 20:01

    tl;dr: for global/public names use AllCaps like XORcist said:

    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.


    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).

    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.

    For this I agree with XORcist in that module level "alias" references should adhere to your class naming standard, probably AllCaps:

    class MyClass(object):
        pass
    
    # good
    ReferenceToClass = MyClass
    

    However when it comes to parameter and variable names, supposedly lowercase_underscores should 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:

    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". However in that latter case I propose to avoid the ambiguity with descriptive naming yet again. For example, you may use a classobj suffix:

    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.

    0 讨论(0)
  • 2021-02-03 20:10

    On module level the second:

    ReferenceToClass = MyClass

    As a function argument, the first:

    reference_to_class = MyClass

    0 讨论(0)
提交回复
热议问题