is it a bug ??

brauner joel jbrauner at wanadoo.fr
Wed Jan 29 15:54:56 EST 2003


as expected, the following function :

def g(a=[]):
	print a
	if len(a)<5:
		a = a + [1]
		g(a)
	print a

returns:

 >>> g()
[]
[1]
[1, 1]
[1, 1, 1]
[1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1]
[1, 1, 1]
[1, 1]
[1]

but the same function with the use of the "append" methods :

def f(a=[]):
	print a
	if len(a)<5:
		a.append(1)
		f(a)
	print a

returns :

[]
[1]
[1, 1]
[1, 1, 1]
[1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]


In other words, the recursive function f seems to make the a list global 
for all the occurences of the function

is that normal ?


PS : excuse my english-speaking





More information about the Python-list mailing list