Home > database >  What is meant by "object." that prefixes method names in the Python docs?
What is meant by "object." that prefixes method names in the Python docs?

Time:12-13

I've been intensively consulting the Python docs lately, and I can't figure out what the object. prefix means. For example, I've been reading about:

 __getitem__()

and in the docs its definition header is written as:

 object.__getattribute__(self, name)

Here's the link to its entry.

What added to the confusion is that running:

object.__getattribute__(self, name)

raises the error:

type object 'object' has no attribute '__getattr__()'

Update,,

I also considered the possibility that it's there to denote instance of a class, but then the self parameter becomes invalid.. .

CodePudding user response:

object is only base class for all Python objects. Every class what you create will be subclass of it. You can't call its attributes itself. Referring to @Brian's comment you can read about it in built-in help.

CodePudding user response:

You're looking at the data model docs. In those docs, object. is basically a placeholder. It doesn't refer to the object class. object doesn't have most of the methods in those docs.

If there was a meaningful class name they could have used, they would have done so, but these docs aren't specific to any particular class. The docs are saying, if a class has this method, this is what the Python language internals will assume it means.

CodePudding user response:

object is the base class of all Python types.

As for the error you got, the documentation for Special method names does not describe pre-defined methods. Instead, it describes callbacks that you can define on your own classes. Also, __getattribute__ is not the same as __getattr__. When I type dir(object) in the Python REPL, I see it has a __getattribute__ attribute, but no __getattr__.

I'm not sure why that documentation prefixes the method names with object.. I guess the object there is supposed to be any object, not just the class.

  • Related