简体   繁体   中英

python class object definition

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.

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, eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM