Capitalization for variable that holds a class
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Sun Sep 23 17:42:23 EDT 2012
On Mon, 24 Sep 2012 02:12:25 +1000, 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]
>>
>> Fight.
>>
>> (When considering, substitute in a more real-world example like
>> [Token() for Token in allTokens] or [token() for token in allTokens])
>
> An interesting point! I assume you're basing this on the PEP 8
> recommendation: http://www.python.org/dev/peps/pep-0008/#class-names
>
> 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. And certainly a long-lived variable ought to
> be named in CapWords. However, all you're really doing is taking a bunch
> of callables, calling them, and making a list of the results. I'd
> therefore be inclined to _not_ capitalize it. YMMV though.
The difference is in the programmer's intention. I would go with:
[aclass() for aclass in MyClasses]
to emphasise that aclass is intended as a temporary loop variable and not
a long-lasting class definition.
So I guess I'm agreeing with Chris.
--
Steven
More information about the Python-list
mailing list