How can a function refer stably to itself?

Viewed 1427

[The code in the original version was badly messed up. Even after I fixed the code, several highly confusing typos remained in the post. I believe I finally fixed all of them too. Profuse apologies.]

The two calls to alias below produce different outputs, because the object associated with the variable my_own_id changes between the two calls:

>>> def my_own_id():
...     me = my_own_id
...     return id(me)
... 
>>> alias = my_own_id
>>> alias()
4301701560
>>> my_own_id = None
>>> alias()
4296513024

What can I assign to me in the definition of my_own_id so that its output remains invariant wrt subsequent re-definitions of the my_own_id variable? (IOW, so that the internal me variable always refers to the same function object?)

(I can get the current frame (with inspect.currentframe()), but it contains only a reference to the current code object, not to the current function.)

P.S. The motivation for this question is only to know Python better.

7 Answers
Related