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.

3
  • 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. Commented Jul 18, 2015 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. Commented Jul 19, 2015 at 0:16
  • You're asking about somewhat dark magic here Commented Jul 19, 2015 at 1:32

1 Answer 1

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.

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

4 Comments

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.
@JoeBigler The CPython data model implementation (and everything pertaining to objects) can be found at github.com/python/cpython/tree/master/Objects. Is there a specific method you are curious about ?
"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.
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 github.com/python/cpython/blob/….

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.