1

I was surprised to see that in Python, the classes in the argument list of a function, are instantiated when the function is defined and not when the function is called.

Consider the following code

   class A:
       def __init__(self):
           print("init method called")

   def f(a=A()):
       print("function f called")

   f()
   f()

This will give the following output:

init method called
function f called
function f called

Thus, only one object of class A is constructed in this program, and not one for each call of function f. Why is this, and is it a common trait for interpreted language?

Morten
  • 2,148
  • 2
  • 15
  • 16

1 Answers1

0

You can take a look at the documentation or at other questions with similar topics:

Sparky05
  • 4,692
  • 1
  • 10
  • 27
  • Thanks, Sparky05 I especially found this site insight full: http://effbot.org/zone/default-values.htm since I actually had the 'default value case' (I just din't knew it was a thing and what was the best way to handle it). – Morten Aug 28 '19 at 09:14