-2

I'm learning the lambda function in Python.

If I write a lambda expression line without assign it to some function name, can I call that lambda function by its address in memory or id later?

Or can I have some "pointer" like what is in C to point to that part of memory?

In the picture below, you can see that I wrote the lambda expression twice and found that they have the same address in memory, yet if I assign "lambda x:x**2" to a variable a, the address is different.

I want to use that lambda function "lambda x:x**2" according to its address in memory.

Thanks.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Al.
  • 1
  • 2
  • 4
    If you want to refer to a function, assign it via `f = lambda x: x` or better still use `def`, if your lambda function isn't anonymous, you're probably doing it wrong, plus using the memory address is going to to rely on implementation details at best – Chris_Rands Aug 03 '18 at 09:48
  • Thx, I know that I can use f = lambda x: x to assign the lambda expression to some variable, but I wonder if I can assign the address in memory to some variable. You know, once I wrote that line "lambda x:x**2" down, Python will tell me the type and address of this expression, but then I can never use this function object again. So, I wonder if I can give an object a name according to its address in memory to call or use it. – Al. Aug 03 '18 at 09:56
  • @AI. cf my anwer. – bruno desthuilliers Aug 03 '18 at 10:03
  • Welcome to Stackoverflow! Please post code as text, not as images. Images cannot be indexed by search engines and therefore cannot be found by other users. Thanks! – Maximilian Peters Aug 03 '18 at 12:05

1 Answers1

0

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.

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118