Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 685
Peter Hudec Avatar asked Dec 07 '12 15:12

Peter Hudec


People also ask

What is the naming convention for class names?

Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).

What is a convention in Python?

Convention means a certain way in which things are done within a community to ensure order. Similarly, Coding conventions are also a set of guidelines, but for a programming language that recommends programming style, practices, and methods.

How should Python files be named?

Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.


2 Answers

On module level the second:

ReferenceToClass = MyClass

As a function argument, the first:

reference_to_class = MyClass

like image 155
XORcist Avatar answered Sep 25 '22 02:09

XORcist


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.

like image 45
n611x007 Avatar answered Sep 21 '22 02:09

n611x007