Python static variable generator function -
i using following function generate static variable in python:
#static variable generator function def static_num(self): k = 0 while true: k += 1 yield k
when calling function main code:
regression_iteration = self.static_num() print " completed test number %s %s \n\n" % (regression_iteration, testname)
i output:
"completed test number <generator object static_num @ 0x027be260> be_sink_ncq"
why not getting incremented integer? static variable generator going wrong?
edit:
i calling function static_num in following manner now:
regression_iteration = self.static_num().next()
but returns '1' since value of 'k' being initialized 0 every time function called. therefore, not required output 1,2,3,4 .... on every call of function
its hard whether need use approach -- strongly doubt it, instead of generator abuse using mutable types default initializers:
def counter(init=[0]): init[0] += 1 return init[0] x = counter() print(x) # 1 print(x) # 1 print(x) # 1 x = counter() print(x) # 2 print(x) # 2 print(x) # 2 # ... etc
the return value of counter
increases 1 on each call, starting @ 1.
Comments
Post a Comment