self question

Schüle Daniel uval at rz.uni-karlsruhe.de
Tue Jul 25 14:08:32 EDT 2006


dan.gass at gmail.com schrieb:
>> 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

yes, I haven't thought of that
nowI changed my class to

class Graph:
	settings = {
		"NumNodes" : 10,
		"MinNodes" : 2,
		"MaxNodes" : 5
	}
	def randomizeEdges(self,
		lowhigh = (settings["MinNodes"], settings["MaxNodes"])):
		low, high = lowhigh
		for node in self.nodes:
			x = random.randint(low, high)
			# link the nodes


maybe the only minor point is that no relationship
can be expressed

settings = {
	"NumNode" : 10,
	"MinNode" : settings["NumNode"] / 2,
	"MaxNode" : settings["NumNode"]
}

but I think the solution is nevertheless ok


Regards, Daniel



More information about the Python-list mailing list