Hello fellows...

Is there something like static function variables in python?
I know there are static attributes of a class, but what if i want
to use a static variable inside a function?
somthing like this (stupid) code in c++ for example:

int static_num()
{
     static int x = 0;
     return ++x;
}

void main()
{
     for (int i=0; i<10; i++)
     {
           cout << static_num() << endl;
     }
}

If there aren't such static variables , is there a way for doing something similar to this c++ code?

thanks a lot!

Recommended Answers

All 9 Replies

x=0
def static_num() :
   global x
   x=x+1
   return x

for i in range(0,10) :
     print static_num

I always learn that the use of global variables is to be discouraged. Is this the only way to copy static variable behaviour in Python?

Bumsfeld you are right, avoid globals, they do lead to errors. Using a generator function avoids globals ...

def static_num2():
    k = 0
    while True:
       k += 1
       yield k

static = static_num2().next

for i in range(0,10) :
    print static()

I knew there had to be a way to avoid these global variables, I am still learning a lot, generator functions are somewhat confusing right now!

Hmm. There are a couple of ways you could simulate the effect of static variables - but they aren't explicitly in the language.

One way is to abuse the fact that default parameters for functions are evaluated only once when the function is defined, and then kept for the rest of time - so that if one makes the default value a new list or dictionary then it can be used to share values between any running version of the function. For example:

def last_argument(n, arg_store = []):
    "Return the argument used when the function was last called"
    arg_store.append(n)
    if len(arg_store) == 1:
        return -1
    else:
        return arg_store[-2]

I'm not sure whether this would be classed as a 'good solution' or not... I don't know whether it counts as confusing to use stored default arguments for sharing state - also you are never going to use this parameter.

As an alternative you could explicit give the function somewhere to store its state each time it's called. We could do this using an explicit paramter, a class or an instance.

For a parameter we'd effectively use the same as above, apart from we wouldn't specify a default value. We'd then call

state = []
last_argument(1, state)
last_argument(2, state)

The code for a class would use a class method for flair.

class A(object):
    last = -1
    @classmethod
    def last_argument(cls, n):
        temp = cls.last
        cls.last = n
        return temp

A.last_argument(1)
A.last_argument(17)...

The code an instance of a class is much as you would expect.

The default argument approach is probably the approach closest to a static variable, the class method might feel the 'cleanest' - depending on one's bigotries.

Tom

def egg(static={"count":0}):
    static["count"]+=1
    return static["count"]

print egg()
print egg()
# >>> 1
# >>> 2
commented: it's not a good idea for this question +0

python have static variables??????????

No it doesn't, but you can emulate static variables with default arguments, as shown above. This is a side effect of mutable types like dictionaries.

Hi,

You can use functions like this:

def static_num():
    static_num.x += 1
    return static_num.x
static_num.x = 0

if __name__ == '__main__':
    for i in range(10):
        print static_num()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.