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

8
  • What are you trying to do with this generator? And how does your concept of a static number work? Commented Mar 17, 2015 at 6: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 post. Commented Mar 17, 2015 at 6: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? Commented Mar 17, 2015 at 6: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 Commented Mar 17, 2015 at 6: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. Commented Mar 17, 2015 at 6:49

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

jedwards, this worked perfectly !! Thanks a lot for your support.
@user3565150 No problem, and sorry for the confusion in comments. And thank you for pushing me over 10k rep :)
Its fine, you have helped me a lot before, I can take this little pain :)
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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.