Consider the following Python shell session (Python 3.6.2, Win64):

>>> def givemetwo():
...         x = 'two'
...         print(id(x))
...
>>> givemetwo()
1578505988392

So far fine. My understanding of object existence made me
think that the object referred to by x would be deleted when
the givemetwo() function returned, like a local variable in C.

However, this isn't true, as shown by the following in the same
session:

>>> import ctypes
>>> print (ctypes.cast(1578505988392, ctypes.py_object).value)
two

This shows that the object still exists, which was a surprise.

Will this object ever be deleted? I'm learning about function
decorators which, as my early studies tell me, depend on calling
a function defined inside another function. This suggests that
objects defined inside functions are never deleted, otherwise
function decorators would never work. (I'm not 100% sure
my understanding of function decorators is correct since I'm
still learning about them).

What's the right way to think about this?

Cordially,
Jon Forrest

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to