943,947 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 79814
  • Python RSS
Sep 27th, 2005
0

static variables in python

Expand Post »
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:

Python Syntax (Toggle Plain Text)
  1. int static_num()
  2. {
  3. static int x = 0;
  4. return ++x;
  5. }
  6.  
  7. void main()
  8. {
  9. for (int i=0; i<10; i++)
  10. {
  11. cout << static_num() << endl;
  12. }
  13. }

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

thanks a lot!
Last edited by peter_budo; Jun 1st, 2011 at 5:52 am. Reason: Adding code tags to old post
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Avner .H. is offline Offline
18 posts
since Aug 2005
Sep 27th, 2005
0

Re: static variables in python

Python Syntax (Toggle Plain Text)
  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
Reputation Points: 11
Solved Threads: 6
Junior Poster
CrazyDieter is offline Offline
106 posts
since Jul 2005
Sep 30th, 2005
0

Re: static variables in python

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?
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Oct 3rd, 2005
0

Re: static variables in python

Bumsfeld you are right, avoid globals, they do lead to errors. Using a generator function avoids globals ...
python Syntax (Toggle Plain Text)
  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
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 3rd, 2005
0

Re: static variables in python

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!
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Dec 2nd, 2007
0

Re: static variables in python

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:

python Syntax (Toggle Plain Text)
  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

python Syntax (Toggle Plain Text)
  1. state = []
  2. last_argument(1, state)
  3. last_argument(2, state)


The code for a class would use a class method for flair.
python Syntax (Toggle Plain Text)
  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]
taw
Reputation Points: 10
Solved Threads: 2
Newbie Poster
taw is offline Offline
3 posts
since Dec 2007
Sep 12th, 2010
-1
Re: static variables in python
python Syntax (Toggle Plain Text)
  1. def egg(static={"count":0}):
  2. static["count"]+=1
  3. return static["count"]
  4.  
  5. print egg()
  6. print egg()
  7. # >>> 1
  8. # >>> 2
Last edited by ychaouche; Sep 12th, 2010 at 10:24 am. Reason: [code] tag
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ychaouche is offline Offline
2 posts
since Sep 2010
Sep 12th, 2010
0
Re: static variables in python
python have static variables??????????
Reputation Points: 10
Solved Threads: 1
Newbie Poster
zark_yoc is offline Offline
15 posts
since Sep 2010
Sep 12th, 2010
0
Re: static variables in python
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ychaouche is offline Offline
2 posts
since Sep 2010
Oct 27th, 2010
0
Re: static variables in python
Hi,

You can use functions like this:
Python Syntax (Toggle Plain Text)
  1. def static_num():
  2. static_num.x += 1
  3. return static_num.x
  4. static_num.x = 0
  5.  
  6. if __name__ == '__main__':
  7. for i in range(10):
  8. print static_num()
Reputation Points: 10
Solved Threads: 1
Newbie Poster
zaghaghi is offline Offline
3 posts
since Jun 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: knapsack
Next Thread in Python Forum Timeline: grades





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC