Object parent code


#1



I'm not stuck... just have a question. When we create a class, we put "object" in parenthesis. Does that mean we are inheriting from a class named, "object" that is built in to Python? If so, what does the code for that parent class look like? What does it do?


Replace this line with your code.


#2

One can poke around a bit in python's code..

Clone the repository

$ hg clone https://hg.python.org/cpython
$ hg checkout 2.7


Then search for "object", I figure it needs to be named, so it should be in a string

$ ack '"object"'
Python/bltinmodule.c
2707:    SETBUILTIN("object",                &PyBaseObject_Type);

That gives me a variable name, PyBaseObject_Type


Find out where that variable was defined:

$ ack PyBaseObject_Type
Objects/typeobject.c
3489:PyTypeObject PyBaseObject_Type = {

So there's a filename and a line number. All that remains is to open up that file and have a look.


Figure out which commit I've been searching in:

$ hg log --branch 2.7 --limit 1
changeset:   102543:09475e6135d0

Add that commit to the url of the repository where you can browse those files
https://hg.python.org/cpython/file/09475e6135d0/
From there you can navigate to Objects/typeobject.c, line 3489
As you'll find, it's just a list of attributes, most defined elsewhere. You can find them by searching for text in the repository, like I did above.


#3

Whoa, that's cool. Thank you.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.