In lack of a specific covering of this case in PEP 8, one can make up an argument for both sides of the medal:
One side is: As A
and B
both are variables as well, but hold a reference to a class, use CamelCase (TargetClass
) in this case.
Nothing prevents you from doing
class A: pass
class B: pass
x = A
A = B
B = x
Now A
and B
point to the respectively other class, so they aren't really fixed to the class.
So A
and B
have the only responsibility to hold a class (no matter if they have the same name or a different one), and so has TargetClass
.
In order to remain unbiased, we as well can argue in the other way: A
and B
are special in so far as they are created along with their classes, and the classes' internals have the same name. In so far they are kind of "original", any other assignment should be marked special in so far as they are to be seen as a variable and thus in lower_case
.
The truth lies, as so often, somewhere in the middle. There are cases where I would go one way, and others where I would go the other way.
Example 1: You pass a class, which maybe should be instantiated, to a method or function:
def create_new_one(cls):
return cls()
class A: pass
class B: pass
print(create_new_one(A))
In this case, cls
is clearly of very temporary state and clearly a variable; can be different at every call. So it should be lower_case
.
Example 2: Aliasing of a class
class OldAPI: pass
class NewAPI: pass
class ThirdAPI: pass
CurrentAPI = ThirdAPI
In this case, CurrentAPI
is to be seen as a kind of alias for the other one and remains constant throughout the program run. Here I would prefer CamelCase
.
variable_style
. I can't see anything immediately in PEP8 that answers this, but thinking of it as a variable makes more sense to me. UsingClassStyle
makes it look like it is the natural name of a class that is defined somewhere else in my code.