[Tutor] Static Variable in Functions

Yaşar Arabacı yasar11732 at gmail.com
Sun Mar 13 20:30:47 CET 2011


Hi,

As I am starting to learn python, I follow dive into python. As I read 
object section, I came across something called class attributes and data 
attributes. Because of the reason that class attributes look and behave 
exactly like static variables in other languages (as I have used in php 
for example.) That made me think that, there should be static variables 
for functions too (or lets call them function variables?!?). Doing a 
little bit research on that, I see that there is no static variable for 
functions but we can make things behave like that as introduced here:

http://www.daniweb.com/software-development/python/threads/33025

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

print egg()
print egg()
# >>> 1
# >>> 2

Author of this post says that we can use mutable variables like this as 
static function variables. I was wondering what are mutable variables 
and what is rationale behind them.



More information about the Tutor mailing list