Sarien
Sarien

Reputation: 6982

When is a new name introduced in Python?

I am asking because of the classic problem where somebody creates a list of lambdas:

foo = []
for i in range(3):
    foo.append((lambda: i))

for l in foo:
    print(l())

and unexpectedly gets only twos as output. The commonly proposed solution is to make i a named argument like this:

foo = []
for i in range(3):
    foo.append((lambda i=i: i))

for l in foo:
    print(l())

Which produces the desired output of 0, 1, 2 but now something magical has happened. It sort of did what is expected because Python is pass-by-reference and you didn't want a reference.

Still, just adding a new name to something, shouldn't that just create another reference?

So the question becomes what are the exact rules for when something is not a reference?

Considering that ints are immutable and the following works:

x = 3
y = x
x = 5
print(x, y) // outputs 5 3

probably explains why adding that named parameter works. A local i with the same value was created and captured.

Now why, in the case of our lambdas was the same i referenced? I pass an int to function and it is refenced and if I store it in a variable it is copied. Hm.

Basically I am looking for the most concise and abstract way possible to remember exactly how this works. When is the same value referenced, when do I get a copy. If it has any common names and there are programming languages were it works the same that would be interesting as well.

Here is my current assumption:

  1. Arguments are always passed to functions by reference.
  2. Assigning to a variable of immutable type creates a copy.

I am asking anyway, just to make sure and hopefully get some background.

Upvotes: 3

Views: 174

Answers (6)

Ben
Ben

Reputation: 71495

The behaviour really has very little to do with how parameters are passed (which is always the same way; there is no distinction in Python where things are sometimes passed by reference and sometimes passed by value). Rather the problem is to do with how names themselves are found.

lambda: i

creates a function that is of course equivalent to:

def anonymous():
    return i

That i is a name, within the scope of anonymous. But it's never bound within that scope (not even as a parameter). So for that to mean anything i must be a name from some outer scope. To find a suitable name i, Python will look at the scope in which anonymous was defined in the source code (and then similarly out from there), until it finds a definition for i.1

So this loop:

foo = []
for i in range(3):
    foo.append((lambda: i))

for l in foo:
    print(l())

Is almost exactly as if you had written this:

foo = []
for i in range(3):
    def anonymous():
        return i
    foo.append(anonymous)

for l in foo:
    print(l())

So that i in return i (or lambda: i) ends up being the same i from the outer scope, which is the loop variable. Not that they are all references to the same object, but that they are all the same name. So it's simply not possible for the functions stored in foo to return different values; they're all returning the object referred to by a single name.

To prove it, watch what happens when I remove the variable i after the loop:

>>> foo = []
>>> for i in range(3):
    foo.append((lambda: i)) 
>>> del i
>>> for l in foo:
    print(l())
Traceback (most recent call last):
  File "<pyshell#7>", line 2, in <module>
    print(l())
  File "<pyshell#3>", line 2, in <lambda>
    foo.append((lambda: i))
NameError: global name 'i' is not defined

You can see that the problem isn't that each function has a local i bound to the wrong thing, but rather than each function is returning the value of the same global variable, which I've now removed.

OTOH, when your loop looks like this:

foo = []
for i in range(3):
    foo.append((lambda i=i: i))

for l in foo:
    print(l())

That is quite like this:

foo = []
for i in range(3):
    def anonymous(i=i):
        return i
    foo.append(anonymous)

for l in foo:
    print(l())

Now the i in return i is not the same i as in the outer scope; it's a local variable of the function anonymous. A new function is created in each iteration of the loop (stored temporarily in the outer scope variable anonymous, and then permanently in a slot of foo), so each one has it's own local variables.

As each function is created, the default value of its parameter is set to the value of i (in the scope defining the functions). Like any other "read" of a variable, that pulls out whatever object is referenced by the variable at that time, and thereafter has no connection to the variable.2

So each function gets the default value of i as it is in the outer scope at the time it is created, and then when the function is called without an argument that default value becomes the value of the i in that function's local scope. Each function has no non-local references, so is completely unaffected by what happens outside it.


1 This is done at "compile time" (when the Python file is converted to bytecode), with no regard for what the system is like at runtime; it is almost literally looking for an outer def block with i = ... in the source code. So local variables are actually statically resolved! If that lookup chain falls all the way out to the module global scope, then Python assumes that i will be defined in the global scope at the point that the code will be run, and just treats i as a global variable whether or not there is a statically visible binding for i at module scope, hence why you can dynamically create global variables but not local ones.

2 Confusingly, this means that in lambda i=i: i, the three is refer to three completely different "variables" in two different scopes on the one line.

The leftmost i is the "name" holding the value that will be used for the default value of i, which exists independently of any particular call of the function; it's almost exactly "member data" stored in the function object.

The second i is an expression evaluated as the function is created, to get the default value. So the i=i bit acts very like an independent statement the_function.default_i = i, evaluated in the same scope containing the lambda expression.

And finally the third i is actually the local variable inside the function, which only exists within a call to the anonymous function.

Upvotes: -2

kindall
kindall

Reputation: 184211

The answer is that the references created in a closure (where a function is inside a function, and the inner function accesses variables from the outer one) are special. This is an implementation detail, but in CPython the value is a particular kind of object called a cell and it allows the variable's value to be changed without rebinding it to a new object. More info here.

The way variables work in Python is actually rather simple.

  1. All variables contain references to objects.
  2. Reassigning a variable points it to a different object.
  3. All arguments are passed by value when calling functions (though the values being passed are references).
  4. Some types of objects are mutable, which means they can be changed without changing what any of their variable names point to. Only these types can be changed when passed, since this does not require changing any references to the object.
  5. Values are never copied implicitly. Never.

Upvotes: 0

Marcin
Marcin

Reputation: 49846

The list of lambdas problem arises because the i referred to in both snippets is the same variable.

Two distinct variables with the same name exist only if they exist in two separate scopes. See the following link for when that happens, but basically any new function (including a lambda) or class establishes its own scope, as do modules, and pretty much nothing else does. See: http://docs.python.org/2/reference/executionmodel.html#naming-and-binding

HOWEVER, when reading the value of a variable, if it is not defined in the current local scope, the enclosing local scopes are searched*. Your first example is of exactly this behaviour:

foo = []
for i in range(3):
    foo.append((lambda: i))

for l in foo:
    print(l())

Each lambda creates no variables at all, so its own local scope is empty. When execution hits the locally undefined i, it is located in the enclosing scope.

In your second example, each lambda creates its own i variable in the parameter list:

foo = []
for i in range(3):
    foo.append((lambda i=i: i))

This is in fact equivalent to lambda a=i: a, because the i inside the body is the same as the i on the left hand side of the assignment, and not the i on the right hand side. The consequence is that i is not missing from the local scope, and so the value of the local i is used by each lambda.

Update: Both of your assumptions are incorrect.

Function arguments are passed by value. The value passed is the reference to the object. Pass-by-reference would allow the original variable to be altered.

No implicit copying ever occurs on function call or assignment, of any language-level object. Under the hood, because this is pass-by-value, the references to the parameter objects are copied when the function is called, as is usual in any language which passes references by value.

Update 2: The details of function evaluation are here: http://docs.python.org/2/reference/expressions.html#calls . See the link above for the details regarding name binding.

* No actual linear search occurs in CPython, because the correct variable to use can be determined at compile time.

Upvotes: 1

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250961

foo = []
for i in range(3):
    foo.append((lambda: i))  

Here since all the lambda's were created in the same scope so all of them point to the same global variable variable i. so, whatever value i points to will be returned when they are actually called.

foo = []
for i in range(3):
    foo.append((lambda z = i: id(z)))

print id(i)      #165618436
print(foo[-1]()) #165618436

Here in each loop we assign the value of i to a local variable z, as default arguments are calculated when the function is parsed so the value z simply points to the values stored by i during the iteration.

Arguments are always passed to functions by reference?

In fact the z in foo[-1] still points to the same object as i of the last iteration, so yes values are passed by reference but as integers are immutable so changing i won't affect z of the foo[-1] at all.

In the example below all lambda's point to some mutable object, so modifying items in lis will also affect the functions in foo:

foo = []
lis = ([], [], [])
for i in lis:
    foo.append((lambda z = i: z))

lis[0].append("bar")
print foo[0]()          #prints ['bar']
i.append("foo")         # `i` still points to lis[-1]
print foo[-1]()         #prints ['foo']

Assigning to a variable of immutable type creates a copy?

No values are never copied.

>>> x = 1000
>>> y = x       # x and y point to the same object, but an immutable object.

>>> x += 1      # so modifying x won't affect y at all, in fact after this step
                # x now points to some different object and y still points to 
                # the same object 1000

>>> x           #x now points to an new object, new id()
1001
>>> y           #still points to the same object, same id()
1000

>>> x = []
>>> y = x              
>>> x.append("foo") #modify an mutable object
>>> x,y             #changes can be seen in all references to the object
(['foo'], ['foo'])

Upvotes: 1

ahuff44
ahuff44

Reputation: 1137

Python doesn't exactly pass by reference or by value (at least, not the way you'd think of it, coming from a language like C++). In many other languages (such as C++), variables can be thought of as synonymous with the values they hold. However, in Python, variables are names that point to the objects in memory. (This is a good explanation (with pictures!)) Because of this, you can get multiple names attached to one object, which can lead to interesting effects.


Consider these equivalent program snippets:

// C++:
int x;
x = 10;  // line A
x = 20;  // line B

and

# Python:
x = 10 # line C 
x = 20 # line D

After line A, the int 10 is stored in memory, say, at the memory address 0x1111.

After line B, the memory at 0x1111 is overwritten, so 0x1111 now holds the int 20


However, the way this program works in python is quite different:

After line C, x points to some memory, say, 0x2222, and the value stored at 0x2222 is 10

After line D, x points to some different memory, say, 0x3333, and the value stored at 0x3333 is 20

Eventually, the orphaned memory at 0x2222 is garbage collected by Python.


Hopefully this helps you get a grasp of the subtle differences between variables in Python and most other languages.

(I know I didn't directly answer your question about lambdas, but I think this is good background knowledge to have before reading one of the good explanations here, such as @Lattyware's)

See this question for some more background info.

Here's some final background info, in the form of oft-quoted but instructive examples:

print 'Example 1: Expected:'
x = 3
y = x
x = 2
print 'x =', x
print 'y =', y

print 'Example 2: Surprising:'
x = [3]
y = x
x[0] = 2
print 'x =', x
print 'y =', y

print 'Example 3: Same logic as in Example 1:'
x = [3]
y = x
x = [2]
print 'x =', x
print 'y =', y

The output is:

Example 1: Expected:

x = 2

y = 3

Example 2: Surprising:

x = [2]

y = [2]

Example 3: Same logic as in Example 1:

x = [2]

y = [3]

Upvotes: 2

Gareth Latty
Gareth Latty

Reputation: 89017

The issue here is how you think of names.

In your first example, i is a variable that is assigned to every time the loop iterates. When you use lambda to make a function, you make a function that accesses the name i and returns it's value. This means as the name i changes, the value returned by the functions also changes.

The reason the default argument trick works is that the name is evaluated when the function is defined. This means the default value is the value the i name points to at that time, not the name itself.

i is a label. 0, 1 and 2 are the objects. In the first case, the program assigns 0 to i, then makes a function that returns i - it then does this with 1 and 2. When the function is called, it looks up i (which is now 2) and then returns it.

In the second example, you assign 0 to i, then you make a function with a default argument. That default argument is the value that is gotten by evaluating i - that is the object 0. This is repeated for 1 and 2. When the function is called, it assigns that default value to a new variable i, local to the function and unrelated to the outer i.

Upvotes: 4

Related Questions