is it a bug ??
Terry Reedy
tjreedy at udel.edu
Wed Jan 29 19:48:42 EST 2003
More information about the Python-list mailing list
Wed Jan 29 19:48:42 EST 2003
- Previous message (by thread): biggles on windows
- Next message (by thread): is it a bug ??
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
> def g(a=[]): > print a > if len(a)<5: > a = a + [1] This creates a new list which is passed on to g, which creates another new list.... > g(a) > print a This prints a *different* list than the first print statement. To better see what is goint on print id(a), a in both places Note: since you pass an explicit argument at every call after the first, the difference you see is not about default args, and using that for the first call only confuses the issue. >>> def g(a): ... print id(a),a ... if len(a)<5: ... a = a + [1] ... g(a) ... print id(a),a ... >>> g([]) 7938416 [] 7962144 [1] 7962096 [1, 1] 7962048 [1, 1, 1] 7962000 [1, 1, 1, 1] 7961952 [1, 1, 1, 1, 1] 7961952 [1, 1, 1, 1, 1] 7961952 [1, 1, 1, 1, 1] 7962000 [1, 1, 1, 1] 7962048 [1, 1, 1] 7962096 [1, 1] 7962144 [1] Note that there is a different list for each length. > def f(a=[]): > print a > if len(a)<5: > a.append(1) This mutates the list in place (and passes it on) > f(a) > print a So all prints print the same list, which never shrinks. Again, print the id also. Terry J. Reedy
- Previous message (by thread): biggles on windows
- Next message (by thread): is it a bug ??
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list