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