Skip to content
Advertisement

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.

Advertisement

Answer

On module level the second:

ReferenceToClass = MyClass

As a function argument, the first:

reference_to_class = MyClass

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement