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?
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.
According to PEP8, function and variable names should be lowercase with words separated by underscores.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With