0

I'm struggling with understanding which methods are called when an old-style object is created.

Doing a little research I found that this differentiation only applies for python 2.7 not for python 3, however, I'm testing the methods in code:

class OldClass():
    pass

class NewClass(object):
    pass

old = OldClass()
new = NewClass()

print(type(old))
print(type(new))
print(old.__class__) #It retrieves __main__
print(old.__new__)  #Never called
print(old.__init__) #Never called

And this the output:

<type 'instance'>
<class '__main__.NewClass'>
__main__.OldClass
Traceback (most recent call last):
File "main.py", line 29, in <module>
print(old.__init__)
AttributeError: OldClass instance has no attribute '__init__'

I'm continuing in research for this topic, here are some links with some information related:

New Style class vs Old style: https://www.youtube.com/watch?v=KwpnXqnVx2o

stack overflow new style vs old style class: What is the difference between old style and new style classes in Python?

If I find something I will update the post

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271

1 Answers1

1

__init__ only exists and is only called if you define it. Since you didn't define __init__, it's not called. When it is defined, it's the only useful point of interception to creating the instance, and most of what you're trying to do works fine.

New-style classes can also use __new__ to hook instance construction (as opposed to initialization), and also allow for metaclasses (which can hook stuff in even crazier ways), but they're not available for old-style classes (defining __new__ doesn't change anything, and using metaclasses implicitly opts in to new-style classes).

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • Thanks for your response @ShadowRanger, I notice that the behavior with `__init__` was different that `__new__` , I have a question. I understand that `__init__` only exists if it is defined but why it is the only one when you print [print old.__main__](https://tio.run/##dY@9DoMwDITn5ikMCyBVHTpW6t6tAw8QRcS0kUyCSCTE06cpgTb0x5vv/Ml3/eTuRh@9b0hYC1eStZsIT2zHIIzEFjjvhNKclxaprYLTD0o7eG57KFYXGkGEsmAJp7Rybw6W@cDj0S9c4/j/62x@Ux1aK26Y/tyQ@QWJDIxmIJllOWOGJJxfvcuKxesgH9ZqGynG3UhzlERZMnj/AA) that results in `<__main__.OldStyle instance at 0x7f2773fe6368> __init__ called` ? – Axel G. Aguilar May 10 '20 at 04:31
  • @AxelG.Aguilar: That's not at all what happened. Your first output is triggered by the `old = OldStyle()` line (which invokes `__init__` implicitly). – ShadowRanger May 10 '20 at 12:13
  • Thanks again for your response :) @ShadowRanger, so If I ask you that which methods are called when an old-style object is created the response is that it depends that if you defined the methods or not. For instance if an old-style method would be created we can't assure that `__init__` or `__new__` or even `__ctor__` methods were called? By now my answer would be that `__init__` is the only method that is called when an old-style object is created, what do you think? – Axel G. Aguilar May 10 '20 at 21:53
  • @AxelG.Aguilar: Yes, if I understand you correctly? `__init__` is called if it exists. Nothing else is ever called for old-style class construction/initialization. – ShadowRanger May 11 '20 at 00:14
  • Thanks again @ShadowRanger, so basically the final response is that none method is called when an old-style object is created. And only `__init__` method wold be called but only if it is defined in old-style object right? – Axel G. Aguilar May 11 '20 at 13:09