Skip to main content
edited body
Source Link
RocketDonkey
  • 37.4k
  • 8
  • 83
  • 84

There are discussions as to why this method isn't the best to determine efficacy of approaches, but if you use dis to inspect the bytecode of the functions, you can see that they are structured in different ways, namely that t1 evaluates its keyworddefault argument at the time it is defined, and therefore does not require it to be redefined on subsequent function calls:

>>> import dis >>> def t1(A=1): ... pass >>> def t2(): .... A=1 >>> dis.dis(t1) 2 0 LOAD_CONST 0 (None) 3 RETURN_VALUE >>> dis.dis(t2) 2 0 LOAD_CONST 1 (1) 3 STORE_FAST 0 (A) 6 LOAD_CONST 0 (None) 9 RETURN_VALUE 

There are discussions as to why this method isn't the best to determine efficacy of approaches, but if you use dis to inspect the bytecode of the functions, you can see that they are structured in different ways, namely that t1 evaluates its keyword argument at the time it is defined, and therefore does not require it to be redefined on subsequent function calls:

>>> import dis >>> def t1(A=1): ... pass >>> def t2(): .... A=1 >>> dis.dis(t1) 2 0 LOAD_CONST 0 (None) 3 RETURN_VALUE >>> dis.dis(t2) 2 0 LOAD_CONST 1 (1) 3 STORE_FAST 0 (A) 6 LOAD_CONST 0 (None) 9 RETURN_VALUE 

There are discussions as to why this method isn't the best to determine efficacy of approaches, but if you use dis to inspect the bytecode of the functions, you can see that they are structured in different ways, namely that t1 evaluates its default argument at the time it is defined, and therefore does not require it to be redefined on subsequent function calls:

>>> import dis >>> def t1(A=1): ... pass >>> def t2(): .... A=1 >>> dis.dis(t1) 2 0 LOAD_CONST 0 (None) 3 RETURN_VALUE >>> dis.dis(t2) 2 0 LOAD_CONST 1 (1) 3 STORE_FAST 0 (A) 6 LOAD_CONST 0 (None) 9 RETURN_VALUE 
Source Link
RocketDonkey
  • 37.4k
  • 8
  • 83
  • 84

There are discussions as to why this method isn't the best to determine efficacy of approaches, but if you use dis to inspect the bytecode of the functions, you can see that they are structured in different ways, namely that t1 evaluates its keyword argument at the time it is defined, and therefore does not require it to be redefined on subsequent function calls:

>>> import dis >>> def t1(A=1): ... pass >>> def t2(): .... A=1 >>> dis.dis(t1) 2 0 LOAD_CONST 0 (None) 3 RETURN_VALUE >>> dis.dis(t2) 2 0 LOAD_CONST 1 (1) 3 STORE_FAST 0 (A) 6 LOAD_CONST 0 (None) 9 RETURN_VALUE