2

I am using the following function to generate a static variable in Python:

 #Static variable generator function
def static_num(self):
    k = 0
    while True:
        k += 1
        yield k

When I am calling this function from main code:

 regression_iteration = self.static_num()
 print " Completed test number %s  %s \n\n" % (regression_iteration, testname)

I get this output:

  "Completed test number <generator object static_num at 0x027BE260>  be_sink_ncq"

Why I am not getting an incremented integer? Where is my static variable generator going wrong?

Edit:

I am calling the function static_num in the following manner now:

regression_iteration = self.static_num().next()

But it returns only '1' since the value of 'k' is being initialized to zero every time the function is called. Therefore, I do not get the required output 1,2,3,4 .... on every call of the function

user3565150
  • 884
  • 5
  • 21
  • 49
  • What are you trying to do with this generator? And how does your concept of a static number work? – jedwards Mar 17 '15 at 06:25
  • "*Yield is a keyword that is used like return, except the function will return a generator.*" You are printing the whole object not just the values. See [this](http://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do-in-python) post. – Marco Mar 17 '15 at 06:29
  • When you say *static variable*, do you mean a variable that is shared between all instances of a class? If so, how does your generator help accomplish this? – jedwards Mar 17 '15 at 06:32
  • Similar to the concept of static variable in C, I need a variable whose value will remain unchanged when a function is called repeatedly. At the end of the function, I will increase the value of the variable, so that in the next function call it gets an incremented value and does not get reset to zero like an automatic variable – user3565150 Mar 17 '15 at 06:39
  • Why not just *not modify* the value inside the function. I may be misunderstanding, but it seems like you're doing significantly more work than necessary. – jedwards Mar 17 '15 at 06:49
  • @jedwards, I am trying to increment the value of a variable (regression_iteration) on every call of a function. The variable has to reside inside the function. So, it has to be a static variable incrementing from 1 to 2 to 3 and so on.... – user3565150 Mar 17 '15 at 06:52
  • I don't think you have a clear grasp on the scoping rules. But without more context, it's hard to give you a great answer. – jedwards Mar 17 '15 at 06:59
  • From your edit, it looks like you could just drop in my 3-line `counter` function and write `regression_iteration = counter()` – jedwards Mar 17 '15 at 07:00

3 Answers3

4

Its hard to say whether you need to use this approach -- I strongly doubt it, but instead of a generator you could abuse using mutable types as 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 by one on each call, starting at 1.

jedwards
  • 29,432
  • 3
  • 65
  • 92
0

The yield keyword convert your function return value from int to an int generator, so you have to use "for i in self.static_num()" to access the int.If you want to access the generator one by one. Use next function.

amow
  • 2,203
  • 11
  • 19
0

Assuming that by static you meant static to the class, the best way to do this is to use a class variable and a classmethod:

class Static_Number_Class(object):
    _static_number = 0
    @classmethod
    def static_number(cls):
        cls._static_number += 1
        return cls._static_number

If this is just a stand alone function you could use a closure instead:

def Counter():
    n = 0
    def inner():
        n += 1
        return n
    return inner

count = Counter()
x = count()
print(x) # 1
x = count()
print(x) # 2
Matt
  • 724
  • 4
  • 11