Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Static variables in Python?

Status
Not open for further replies.

mikevh

Programmer
Apr 23, 2001
1,033
US
I've been teaching myself Python for about 3 weeks.

Does Python have something equivalent to a static variable in C, i.e., a variable local to a function that will retain its value across calls? The closest thing I've been able to come up with is a global variable that's modified within the function, e.g.
Code:
prtlines_first=True # initialized at module level

def prtlines(line):
    global prtlines_first
    if prtlines_first:
        # do something first time function is called
    else:
        prtlines_first=False
    # rest of function
Is there a better way to do this? Thanks.
 
Whoops, code should be like so, with no else clause
Code:
prtlines_first=True # initialized at module level

def prtlines(line):
    global prtlines_first
    if prtlines_first:
        # do something first time function is called
        prtlines_first=False
    # rest of function
 
Here is an alternative using a variable local to the function:

Code:
def prtlines(line, isfirst = [ True ]):
    if isfirst[0]:
        # do something first time function is called
        isfirst = [ False ]
    # rest of function

Call the function using:
prtlines(line)

If you want to reset and do again the action of the first line you can use:
prtlines(line, True)

For alternative methods to handle the problem (iterators, ...), see:
 
Well, all variables have a name space. If you just have a global function, it will take a global variable in order for it to keep it's value. If you put the function in a class, you can set a static class attribute.

Code:
>>> class Line_Printer:
	first=True
	def __init__(self):
		self.first=True
	def prt_lines(self):
		print "Static: ",
		if Line_Printer.first:
			print "true."
		else:
			print "false."
		print "Non-Static: ",
		if self.first:
			print "true."
		else:
			print "false."

			
>>> lp=Line_Printer()
>>> lp.prt_lines()
Static:  true.
Non-Static:  true.
>>> lp.first=False
>>> lp.prt_lines()
Static:  true.
Non-Static:  false.
>>> lp2=Line_Printer()
>>> lp2.prt_lines()
Static:  true.
Non-Static:  true.
>>> Line_Printer.first=False
>>> lp.prt_lines()
Static:  false.
Non-Static:  false.
>>> lp2.prt_lines()
Static:  false.
Non-Static:  true.
>>>

Classes were invented to do what you are trying to do with a stand-alone function. If you want to combine code and data, that's what an object is for! :)

As you can see from the example, you probably just want to use the "Non-Static" variable (just use self. to get to it). If you need a variable thats the same across all objects of a class, you can just set it the same way you create a function (directly on the class itself).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top