self question

dan.gass at gmail.com dan.gass at gmail.com
Tue Jul 25 13:45:59 EDT 2006


> cnt = 1
> def foo():
> 	global cnt
> 	cnt += 1
> 	return cnt
>
> def bar(x=foo()):
> 	print x
>
> bar()	# 2
> bar()	# 2
> bar()	# 2

Looks to me like you want to use the following programming pattern to
get dynamic default arguments:

cnt = 1
def foo():
	global cnt
	cnt += 1
	return cnt

def bar(x=None):
	if x is None:
		x = foo()
	print x
 
bar()	# 2
bar()	# 3
bar()	# 4




More information about the Python-list mailing list