Lambda function address in python

I have it:

>>> a = lambda : lambda x : x * x 

This gives me a permanent address every time:

 >>> a <function <lambda> at 0x7f22769e76e0> >>> a <function <lambda> at 0x7f22769e76e0> >>> a <function <lambda> at 0x7f22769e76e0> 

However, it is not. Why is that? Also note that it gives only two addresses? Why is that? Is the internal lambda function created on the fly and is returned every time a() called? Wasn't it created when it was announced?

 >>> a() <function <lambda> at 0x7f22769e7320> >>> a() <function <lambda> at 0x7f22769e75f0> >>> a() <function <lambda> at 0x7f22769e7320> >>> a() <function <lambda> at 0x7f22769e75f0> 
+4
source share
2 answers

a is the function itself as a variable. Its address will not change if it does not move in some way in memory.

a() actually calls the function. The function will not do anything until it is called via () . So yes, here your inner lambda function is created on the fly. These will be the addresses that you see there. As Blckknght comments, garbage collection probably leads to memory reuse.

+2
source

Yes, your understanding is correct: an β€œinternal” lambda is created only when an β€œexternal” lambda is evaluated, which does not happen until it is called (external).

+1
source

Source: https://habr.com/ru/post/1496937/


All Articles