I've tried searching online for this question but because the word "object" is so common I get lots of unrelated results instead of what I'm looking for. I also looked through the official docs here: https://docs.python.org/3/tutorial/classes.html and didn't find any explanation for this. So please don't freak out when you read this question.
Question:
In Python while declaring a new class we extend the object class. For ex:
class SomeClass(object):
#eggs and ham etc
Here we notice that SomeClass
has a capital S because we are following camel case. However, the class that we are inheriting from - "object
" doesn't seem to follow this naming convention. Why is the object class in all lower case?
All Python's built-in types have lower case: int, str, unicode, float, bool, etc. The object type is just another one of these.
https://www.python.org/dev/peps/pep-0008/#class-names says:
Class Names
Class names should normally use the CapWords convention.
The naming convention for functions may be used instead in cases where the interface is documented and used primarily as a callable.
Note that there is a separate convention for builtin names: most builtin names are single words (or two words run together), with the CapWords convention used only for exception names and builtin constants. [emphasis mine]
All other classes should use the CapWorlds convention. As list
, object
, etc are built-in names, they follow this separate convention.
(copied from my answer to If the convention in Python is to capitalize classes, why then is list() not capitalized? Is it not a class?)
If you go to the python interpreter and do this:
>>> object
<type 'object'>
You'll see object is a built-in type, the other built-in types in python are also lowercase type, int, bool, float, str, list, tuple, dict, ...
. For instance:
>>> type.__class__
<type 'type'>
>>> object.__class__
<type 'type'>
>>> int.__class__
<type 'type'>
>>> type.__class__
<type 'type'>
>>> int.__class__
<type 'type'>
>>> bool.__class__
<type 'type'>
>>> float.__class__
<type 'type'>
>>> str.__class__
<type 'type'>
>>> list.__class__
<type 'type'>
>>> tuple.__class__
<type 'type'>
>>> dict.__class__
<type 'type'>
So it makes sense they are not lowercase, that way is quite easy to distinguish them from the other type of classes
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