8

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?

serv-inc
  • 32,612
  • 9
  • 143
  • 165
Mugen
  • 1,255
  • 5
  • 19
  • 39
  • 1
    Though `Exception` is a built-in class with the `e` being capital `E`. I assume Python followed the lower-case letter convention for datatypes and I'm not sure about that to confirm. – direprobs Sep 06 '16 at 15:01
  • Possible duplicate of [If the convention in Python is to capitalize classes, why then is list() not capitalized? Is it not a class?](https://stackoverflow.com/questions/14973963/if-the-convention-in-python-is-to-capitalize-classes-why-then-is-list-not-cap) – serv-inc Oct 15 '17 at 09:03
  • 1
    @direprobs: it's in the PEP. see my answer – serv-inc Oct 15 '17 at 09:22

4 Answers4

9

All Python's built-in types have lower case: int, str, unicode, float, bool, etc. The object type is just another one of these.

Daniel Roseman
  • 567,968
  • 59
  • 825
  • 842
  • object does look like a keyword. However, on searching a little more I came across this a class **defaultdict** inside the collections package. "defaultdict" is a class that extends the dict class. It's not a built-in and yet it's been declared in small letters. Isn't there something wrong here or are some Python classes really declared in lower cases? – Mugen Sep 05 '16 at 12:35
  • @Mugen `OrderedDict` is written in camel case. The difference is in the Python version they appeared in: 2.5 for `defaultdict` and 2.7 for `OrderedDict`. I don't know if it is related... – Frodon Sep 05 '16 at 13:04
  • @Mugen Btw this question is also covered in [this answer](http://stackoverflow.com/questions/25597121/why-is-ordereddict-named-in-camel-case-while-defaultdict-is-lower-case). – Frodon Sep 05 '16 at 13:11
7

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?)

Community
  • 1
  • 1
serv-inc
  • 32,612
  • 9
  • 143
  • 165
4

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

BPL
  • 9,532
  • 7
  • 47
  • 105
  • 1
    _"You'll see object is a built-in type, not a class"_ types and classes are merged and technically a type is a class and a class is a type. There's a circular relationship in Python's model. – direprobs Sep 05 '16 at 10:54
-6

The short and basic answer is, class is just a keyword used by the python interpreter to know when some thing needs to be seen as a class , like how you have "def" for defining a function. Its a keyword to describe what follows is all.