56

When I create a module with its sole content:

class Classname(randomobject):
    pass

And I try to run the .py file of the module the interpreter says that randomobject is not defined.

But when I do:

class Classname(object):
    pass

The module runs just fine. So if object is not a keyword, then what is it?

2
  • Here's another question which addresses the Python object identifier: python class inherits object. Commented Jan 3, 2013 at 22:16
  • 5
    For the record, stackoverflow.com/q/4015417/404469 is not a duplicate. It's about the mechanism of inheritance; this question is about, and has multiple answers about, the syntactic structure of what's going on. Commented Jun 18, 2017 at 16:54

4 Answers 4

32

object is a (global) variable. By default it is bound to a built-in class which is the root of the type hierarchy.

(This leads to the interesting property that you can take any built-in type, and use the __bases__ property to reach the type called object).

Everything built-in that isn't a keyword or operator is an identifier.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. I assume you can't view the code what is being assigned to this global variable?
@Bentley4 I don't know what you mean.
I'm sorry. In /usr/lib/python2.7 you can find the standard python modules. By opening the python files yourself you can see variable assignments, bodies of the classes and functions etc. of the modules that you can't see by using the help function. Is there any way to access the builtin module and look at the object variable in that module?
@Bentley4 I've never tried. Consider grepping for it; if not, you'll have to find it in the C source.
19

The following three class declarations are identical in Python 3

class Classname(object):
    pass

class Classname():
    pass

class Classname:
    pass

Well, there will be minor differences, but not fundamentally important since the object class is the base for all.

If you plan to write Python agnostic code (Python2 and Python3 agnostic) you may use the first declaration.

Comments

8

object is an identifier that refers to a builtin type.

Unlike many other languages, there are no primitive types in Python. Everything is an object, including all data types.

I'm not sure why you expected inheriting from randomobject to work.

3 Comments

Perhaps I wasn't entirely clear; I meant that object doesn't have special syntactic or semantic significance in Python the way, say, int does in Java.
Strictly, the "type of word that object is" is a variable, for that reason.
Updated answer to reflect this.
7

object is the base class from which you inherit when creating a new-style class in Python 2.

2 Comments

It's not necessary to do this in Python 3.x, however. New-style classes are the default.
In Python 3, all classes still inherit from object. In addition, "the type of word that object is" is an identifier.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.