Python: Is it possible to delay execution of default argument values until the class object has been instantiated?

Viewed 25

In the example below, we see C1.__init__() has been executed before either of the class objects have been instantiated. Is there any way to avoid this?

Python 3.7.5 (default, Nov  7 2019, 10:50:52) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> class C1:
...     def __init__(self):
...        print('C1.__init__ executed')
... 
>>> class C2:
...     def meth_C2(self, c1=C1()):
...         print('C2.__init__ executed')
... 
C1.__init__ executed
0 Answers
Related