3

I have a question about the Python Object class. As I understand it, all classes are based on the Object class. I cannot find the class definition for object:

class Object:
    code for methods and attributes.

If I use dir(object), I get this:

dir(object)

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

Is there code somewhere for these methods or isn't it accessible? I am studying Python with O'Reilly and part of the course is to ask a question on a forum and document the answer. I believe I have complied with all your requirements. I have searched the Internet for hours and your site. I have also searched several books. I cannot even find the topic being addressed. I am relatively new to Python and object oriented programming so maybe I am missing something. Perhaps it isn't necessary to know this to solve a problem, but I am surprised I cannot find any comments on the subject.

NightShadeQueen
  • 3,284
  • 3
  • 24
  • 37
Joe Bigler
  • 179
  • 1
  • 8
  • 1
    I'm not sure what to think of classes asking you to post questions here. It's a bit of a cheek, really: I don't mind helping people who really do have programming questions, but asking you to come up with a random question just to post here seems a bit much. – Daniel Roseman Jul 18 '15 at 21:24
  • It's a question I was going to ask them anyway. When I saw it as part of the project, I thought I'd post it here. I like to understand the fundamentals behind the code. It makes it easier for me to think through how it is really works. Most of what I have read says all classes inherit from Object, but not much more. I just wanted to know more about Object. It may not be necessary to create code, but it seems like fundamental subject that would be covered somewhere. I don't mind thinking of some things like a "black box" as long as I know how they work. – Joe Bigler Jul 19 '15 at 00:16
  • You're asking about [somewhat dark magic here](http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python) – NightShadeQueen Jul 19 '15 at 01:32

1 Answers1

2

These are the internal methods of the object type. Per the documentation

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

These are usually implemented in the runtime, e.g. for the CPython interpreter (the default implementation) you can find the code at https://github.com/python/cpython/blob/master/Objects/object.c.

You can also check https://github.com/python/cpython/blob/7862ded73723e4573ed5d13488c07b41ac31a07e/Objects/typeobject.c#L4357, where you'll find how object is defined internally and where the various methods point. Again, these are internals and specific to cpython but will give you a fair point about what's happening under the hood.

You can of course override most of them to customize the behavior of your class.

Tasos Vogiatzoglou
  • 2,393
  • 12
  • 16
  • Thanks, I looked at the code you suggested. It doesn't list everything I get when I use dir(object). I was wondering where that came from and what they did by default. – Joe Bigler Jul 19 '15 at 00:17
  • @JoeBigler The CPython data model implementation (and everything pertaining to objects) can be found at https://github.com/python/cpython/tree/master/Objects. Is there a specific method you are curious about ? – Tasos Vogiatzoglou Jul 19 '15 at 09:47
  • "__class__" and "__init__" are two. What do they do at the base level? They should have two underscores before and after each, but the display hides them. – Joe Bigler Jul 19 '15 at 18:17
  • Init is the initializer of a class instance. __class__ property is a bit more involved. It's the pointer to the class's instance and in the case of cpython is set at https://github.com/python/cpython/blob/7862ded73723e4573ed5d13488c07b41ac31a07e/Objects/typeobject.c#L3652. – Tasos Vogiatzoglou Jul 19 '15 at 18:29