Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If the convention in Python is to capitalize classes, why then is list() not capitalized? Is it not a class?

Often when I see class definitions class Foo:, I always see them start with upper case letters.

However, isn't a list [] or a dict {} or some other built-in type, a class as well? For that matter, everything typed into the Python's IDLE which is a keyword that is automatically color coded in purple (with the Window's binary distribution), is itself a class, right?

Such as spam = list()

spam is now an instance of a list()

So my question is, why does Python allow us to first of all do something like list = list() when nobody, probably, does that. But also, why is it not list = List()

Did the developers of the language decide not to use any sort of convention, while it is the case that most Python programmers do name their classes as such?

like image 539
Leonardo Avatar asked Feb 20 '13 06:02

Leonardo


People also ask

Are classes in Python capitalized?

Python is case sensitive. So “selection” and “Selection” are two different identifiers. Normally class names will begin with capital letters and other identifiers will be all lower case. It is also common practice to start private identifiers with an underscore.

Should Python functions be capitalized?

According to PEP8, function and variable names should be lowercase with words separated by underscores.

How do you capitalize an object in Python?

They are: Python upper() – The Python upper() method returns a string that has all lowercase characters converted to uppercase characters. Python capitalize() – The capitalize() function in Python turns the first character of a string to an uppercase letter and lowercases any remaining characters.


1 Answers

Yes, uppercase-initial classes are the convention, as outlined in PEP 8.

You are correct that many builtin types do not follow this convention. These are holdovers from earlier stages of Python when there was a much bigger difference between user-defined classes and builtin types. However, it still seems that builtin or extension types written in C are more likely to have lowercase names (e.g., numpy.array, not numpy.Array).

Nonetheless, the convention is to use uppercase-initial for your own classes in Python code.

like image 143
BrenBarn Avatar answered Sep 30 '22 08:09

BrenBarn