What is the naming convention for Python class references

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

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

Answers


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:

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.


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


Need Your Help

How do I setup ASP.NET MVC 2 with MySQL?

mysql asp.net-mvc asp.net-membership mysql-connector

Is it possible to setup ASP.NET MVC 2 to work with a MySQL database?

Renaming a file in NERDTREE

vim rename nerdtree

Is it possible to rename a file in NERDTree? I know about the m menu but there is no rename option.