0

Where can I find the source code for the implementation of object.__new__. It shows as "built-in". I would like to see how it works.

>>> object.__new__
<built-in method __new__ of type object at 0x822060>
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
  • It's not duplicate. I'm not asking how to do it. I'm asking where it is, and I found it. Not to mention that question is almost entirely about built-in code written in python not C internal functions – Evan Carroll Sep 02 '19 at 18:01
  • 1
    This would be off-topic as a request for a 3rd-party resource anyway. Stack Overflow does not need to be an index into every open-source code repository. – chepner Sep 02 '19 at 18:02
  • What part of resolution between built-in Python 3 functions and internal names is provided by any open-source code repository? And while you're at, perhaps you should be a little more self-critical if you think Stack Overflow **does** need to be a Q/A site for a proprietary open-source code repository https://stackoverflow.com/q/57171836/124486 – Evan Carroll Sep 02 '19 at 18:08
  • @EvanCarroll The first answer pretty clearly links the repository and mentions where to find the C code for the built-ins. The other question asks "how to get" **and** "how to find" the source code which makes your question a subset of that question in my eyes. – MSeifert Sep 02 '19 at 18:08
  • @MSeifert It has exactly one sentence which is remotely related to this question *"However, many of the built-in types can be found in the Objects sub-directory of the Python source trunk."* Tell me how you go from that to `typeobject.c`. – Evan Carroll Sep 02 '19 at 18:10
  • @EvanCarroll Well, the `typeobject.c` is in the ["Objects sub-directory"](https://github.com/python/cpython/tree/master/Objects). – MSeifert Sep 02 '19 at 18:13
  • ... Yes, and that doesn't really help me because there are like 30 files in that directory many with 10k+ lines. And to say the least, `typeobject` is not what I look for when I'm playing around with objects. Especially when there is `object.c` too – Evan Carroll Sep 03 '19 at 03:31

1 Answers1

1

Python3's object.__new__ is internal (built-in). It's not written in Python. You can find the C code for it as object_new(PyTypeObject *type, PyObject *args, PyObject *kwds) in typeobject.c.

Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
  • 2
    So did you learn anything from that? Most of the function appears to be checking parameters. The action is at the end, `type->tp_alloc(type, 0);` But without much study of the Python C-API that doesn't make much sense. – hpaulj Sep 02 '19 at 19:53
  • @hpaulj That's just memory mgmt. I don't think that's the interesting stuff here. After I found `object.__new__` I wanted the constructor for all non-base objects which is `type_new` (afaik) in the same file. Basic point is that I don't see a way to make one object and instance of a *different* class. – Evan Carroll Sep 03 '19 at 03:29
  • `numpy.ndarray` uses a custom `__new__`. https://docs.scipy.org/doc/numpy/user/basics.subclassing.html#a-brief-python-primer-on-new-and-init – hpaulj Sep 03 '19 at 04:53