Capitalization for variable that holds a class
Tim Chase
python.list at tim.thechases.com
Sun Sep 23 14:36:17 EDT 2012
On 09/23/12 11:12, Chris Angelico wrote:
> On Mon, Sep 24, 2012 at 1:48 AM, Joshua Landau
> <joshua.landau.ws at gmail.com> wrote:
>> Simple question:
>>
>> [myClass() for myClass in myClasses]
>> vs
>> [MyClass() for MyClass in myClasses]
>
> Since there's no difference between a "class" and a "variable
> containing a class" or a "pointer to a class" or any other such
> concept, it makes sense to capitalize MyClass in your example, if you
> are guaranteeing that they're all classes.
Having just written code very much like this in the last 24hr, my
own code reads something like
my_list_of_classes = [
MyClassA,
MyClassB,
MyClassC,
]
instances = [my_cls(args) for my_cls in my_list_of_classes]
I do come down on it being a variable (and thus
lowercase-with-underscores, according to PEP-8) because, well, it's
varying. A class *definition* is generally expected to be
non-varying (or if it does, it's often a code smell) so it gets the
CapCamelCase.
So while ChrisA correctly claims that at the interpreter-level
"there's no difference between a 'class' and a 'variable containing
a class'", I'd say that at a programmer-level, there's a conceptual
difference between "expect this to vary" and "expect this to be the
same". And I try to write my code for other programmers first, and
the interpreter second.
-tkc
More information about the Python-list
mailing list