-1

When we create a python class, sometimes we did not define __int__() for it. So, when creating instance, under the hood, built-in __init__ and __new__ will be called.

class ABC:
    attr1=models.CharField...
    attr2=models.TextField...
    attr3=models.CharField...

When abc = ABC(attr1=.., attr3=.., attr2=..) is executed, in order to find out how those initial arguments are passed in and create instances, I have been searching for a few hours to find out how does default built-in class methods __init__ and __new__ look like, but failed.

Can someone help please ?

Yan Tian
  • 377
  • 3
  • 11
  • 2
    That looks like a Django model, which is decidedly different from a regular class, since it has a [metaclass](https://github.com/django/django/blob/ad6bb20557f5c87de26aeb3afb061af942a8cc17/django/db/models/base.py#L72) that deals with those fields. Are you talking about a plain class, or a Django model class? (You can find `__init__` for Django models [here](https://github.com/django/django/blob/ad6bb20557f5c87de26aeb3afb061af942a8cc17/django/db/models/base.py#L426).) – AKX Dec 28 '21 at 12:02
  • 1
    Additionally, for your example, `abc = ABC(attr1=.., attr3=.., attr2=..)` will raise a `TypeError: ABC() takes no arguments`, since your example has no overridden `__init__`. – AKX Dec 28 '21 at 12:03
  • 1
    The builtin `object.__new__`/`object.__init__` would be somewhere in C code presumably, if you used the CPython flavour. Would that really help you? Basically, by default it doesn't do anything except return an instance of your class, and it doesn't accept any arguments by default (as pointed out above). – deceze Dec 28 '21 at 12:18
  • 1
    Does this answer your question? [Python (and Python C API): \_\_new\_\_ versus \_\_init\_\_](https://stackoverflow.com/questions/4859129/python-and-python-c-api-new-versus-init) – shrewmouse Dec 28 '21 at 12:34

1 Answers1

1

When you create a class it inherits from object. Basically, when you don't define __init__ method in your class, the object.__init__ is used. And you cannot get Python source code for that one becouse it's implemented with C - PyObject_Init. You can find the definition - PyObject_Init on GitHub

voilalex
  • 2,041
  • 2
  • 13
  • 18