The fact you see the same id for both lambda definitions is an implemetation detail - the first lambda is collected immediatly (since there's no reference to it), so the id is free for reuse and happens to be indeed reused for the second one.
And no, Python is not C and activeley prevents any direct memory access. Note that the fact that CPython's objects ids are memory addresses is also an implementation detail - it's not part of the language definition, and other implementation (Jhython for example) are free to use something else for object ids.
If you want to use a lambda (actually a function
- lambda
is only syntactic sugar and yields a function
object just like a regular def
statement) then you have to have a reference to it (as a variable, as a function parameter - which is actually a variable -, as an element from a collection, as an attribute from an object etc).
Note that this isn't specific to functions either, Python functions are objects (hint: everything you can put on a LHS in Python is an object) and the above stands true for all objects.
I wonder if I can give an object a name according to its address in memory to call or use
What do you think foo = lambda x: x*2
is doing ? It "gives an object a name" (or more exactly "binds an object to a name") so you can use it later, and the binding (internally) relies on the object's id / memory address / etc to access the object.
If you hope to understand Python you first need to stop thinking in C concepts and learn Python concepts.