I want to intercept all object creations to exchange the object with my custom object. For example, if I would like to exchange all list creations with my own list implementation (for this example a string with the content "Hello World!"), so that the following works:
>>> some magic
>>> []
'Hello World'
I tried to overwrite the built-in __new__
, but it doesn't work as expected:
>>> import builtins
>>> builtins.__new__ = lambda *args, **kwargs: 'Hello World'
>>> __new__(type([]))
'Hello World'
>>> []
[]