Where can I find language reference documentation on Object class?

Data model section[1] on Python reference doc uses the term object without expanding on the content of object class first. Where can I find language reference documentation on object class?

Only doc section that talks about object class is in tutorial section(ch 9), but that does not talk about the internals of object class.

[1] 3. Data model — Python 3.12.2 documentation

Does this definition in the python glossary help? Glossary — Python 3.12.2 documentation

I am familiar with basic usages. I am looking for Reference style documentation, something very comprehensive.

That’s because it doesn’t talk about the object class[1] it’s taking about the concept of an object.


  1. additional info: the object class does not contain any functionality as stated in the docs Return a new featureless object ↩︎

1 Like

You mean specifically the object class? It has basically no content, so I am unsure what you are hoping to find out.

  • It has __slots__ = () (or better, a C equivalent of that)
  • It has an __hash__ implementation that just returns id(self)
  • It has an __eq__ that just does self is other
  • __repr__ that returns <object object at 0x000001EC79B49A40>
  • __str__ that returns __repr__

And, probably it’s most special behavior, it has __new__ and __init__ definition that ignore all arguments if the other method is overridden to reduce annoying errors when creating subclasses.

Not sure what more information you are looking for?

Yes.

I am trying to find a language reference documentation on object class - which should be a comprehensive documentation on this particular class.

I am familiar with what you’ve shared from currently available tutorials/how to documentation.

Why? What information do you hope is hidden there? The documentation doesn’t explictly list what object does because it’s completely uninteresting and mostly follows from what is nessary for other language features (e.g. repr(obj)) to correctly work

Hmm, where do you get that from? The normal way to override init is to not pass on any arguments, and the normal way to override new is to call object.__new__() without any arguments. Far as I know, they’re perfectly normal functions that take no arguments.

class MyBaseObject:

    def __init__(self):
        pass

    def __new__(cls):
        return super().__new__(cls)


class A(object):
    def __init__(self, a):
        print(f"A.__init__ got {a}")


class B(MyBaseObject):
    def __init__(self, b):
        print(f"B.__init__ got {b}")


A(1) # A.__init__ got 1

B(1) # TypeError: MyBaseObject.__new__() takes 1 positional argument but 2 were given

This is observable behavior: The B class can’t be instantiated because MyBaseObject.__new__ doesn’t accept extra parameters.

Ah, that is curious. I thought that perhaps it just took *args, but it isn’t. I’m not entirely sure what’s going on and I don’t have time right now to tinker, but I’m sure it’s something nonmagical that you can replicate in your own code.

To gain peripheral knowledge.

It is slightly magic:

Ofcourse, it can be replicated, but I wouldn’t expect most people to accept code that does these kinds of checks:

class MyBaseObject:
    def __init__(self, *args, **kwargs):
        if args or kwargs:
            if self.__class__.__init__ is not MyBaseObject.__init__:
                raise TypeError(f"MyBaseObject.__init__ takes one argument, got ...")
            if self.__class__.__new__ is MyBaseObject.__new__:
                raise TypeError("MyBaseObject.__init__ takes one argument, got ...")

    def __new__(cls, *args, **kwargs):
        if args or kwargs:
            if cls.__init__ is MyBaseObject.__init__:
                raise TypeError("MyBaseObject.__new__ takes one argument, got ...")
            if cls.__new__ is not MyBaseObject.__new__:
                raise TypeError("MyBaseObject.__new__ takes one argument, got ...")
        return super().__new__(cls)

The comment above the linked code explains this, talking about the future plans for version … 2.6 xD

Huh. Yeah. So, not technically “magic” (as in, “stuff you can’t normally do”, like having type be a subclass of object while object is an instance of type), but a definite bit of weirdness.

Classes are callable, so they are ‘functions’ in the math sense of the word. All builtin classes are included in the Built-in Functions chapter at the beginning of the Library Reference.

From the linked doc:

object is a base for all classes. It has methods that are common to all instances of Python classes.

Is there any reference doc page that goes over(or atleast lists) these common method?

Are the Basic Customization and Customizing attribute access sections what you are looking for? Even those are not complete (they focus on methods, and do not include for example __slots__) but I think it’s the closest thing there is. For the most part, I would recommend just reading the entire data-model page.

The object class does not have any object-specific, non-special, methods to document. The dunder (special, double-underline) methods of any object are (almost all) listed by dir(obj).

dir(object)
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

These are (mostly) discussed in the customization sections Clint linked just above. I believe __getstate__, __reduce__, and __reduce_ex__ are used by pickle and discussed in its doc. All should be indexed on the underscore page.

1 Like