static variables in python

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2005
Posts: 18
Reputation: Avner .H. is an unknown quantity at this point 
Solved Threads: 0
Avner .H. Avner .H. is offline Offline
Newbie Poster

static variables in python

 
0
  #1
Sep 27th, 2005
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!
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 76
Reputation: CrazyDieter is an unknown quantity at this point 
Solved Threads: 3
CrazyDieter's Avatar
CrazyDieter CrazyDieter is offline Offline
Junior Poster in Training

Re: static variables in python

 
0
  #2
Sep 27th, 2005
  1. x=0
  2. def static_num() :
  3. global x
  4. x=x+1
  5. return x
  6.  
  7. for i in range(0,10) :
  8. print static_num
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 138
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: static variables in python

 
0
  #3
Sep 30th, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,145
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 949
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: static variables in python

 
0
  #4
Oct 3rd, 2005
Bumsfeld you are right, avoid globals, they do lead to errors. Using a generator function avoids globals ...
  1. def static_num2():
  2. k = 0
  3. while True:
  4. k += 1
  5. yield k
  6.  
  7. static = static_num2().next
  8.  
  9. for i in range(0,10) :
  10. print static()
Last edited by vegaseat; Dec 3rd, 2007 at 1:28 am. Reason: changed php tags
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 138
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: static variables in python

 
0
  #5
Oct 3rd, 2005
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!
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 3
Reputation: taw is an unknown quantity at this point 
Solved Threads: 2
taw taw is offline Offline
Newbie Poster

Re: static variables in python

 
0
  #6
Dec 2nd, 2007
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:

  1. def last_argument(n, arg_store = []):
  2. "Return the argument used when the function was last called"
  3. arg_store.append(n)
  4. if len(arg_store) == 1:
  5. return -1
  6. else:
  7. 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

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


The code for a class would use a class method for flair.
  1. class A(object):
  2. last = -1
  3. @classmethod
  4. def last_argument(cls, n):
  5. temp = cls.last
  6. cls.last = n
  7. return temp
  8.  
  9. A.last_argument(1)
  10. 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
Last edited by vegaseat; Dec 3rd, 2007 at 1:34 am. Reason: changed code tags from [\code] to [/code]
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 48403 | Replies: 5
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC