Ok, I started coding this:
lastentry = 'first'
campdata = {'a1'=0,
'b2'=0}
class Someclass:
def on_window1_destroy(self, widget, data=None):
print campdata
def func1(self)
lastentry = 'b2'
def func2(self)
lastentry = 'a1'
def func2(self)
campdata[lastcall] +=1
But then I found out that python strings (and integers) were immutable...
So how do I get around ths?
-
1I don't think you understand what "immutable" means.Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams01/03/2013 12:08:58Commented Jan 3, 2013 at 12:08
-
I doesn't appear you are mutating any strings in that code. What is the problem you are seeing?benno– benno01/03/2013 12:09:02Commented Jan 3, 2013 at 12:09
-
3This has nothing to do with mutability but with scope - global versus local variables.l4mpi– l4mpi01/03/2013 12:09:55Commented Jan 3, 2013 at 12:09
3 Answers 3
I guess your problem is that you want to change the value of the global variable lastentry
by calling func1
or func2
, which doesn't work. The reason it does not work is because the variable is in the global scope, and assigning to the same name inside of a function just creates a local variable with the same name as the global one. To assign to a global variable, you need to declare it as such:
lastentry = "something"
def func1():
global lastentry #tell python to treat references to lastentry as global
lastentry = "somethingelse"
Note that you don't need to do this if all you are doing with the global value is reading it like in your third function. But if you assign to a variable, it is treated as local to its scope - which is normally the surrounding function - if you don't explicitly declare it global (or nonlocal in python3).
Global variables should only be used when neccessary as they add complexity to the code. In your case you can probably refactor your code to use an instance variable or class variable for lastentry instead of a global one.
-
@EOL, I developed a bit of an eye for it because the distinction between global and local variables seems to be one of the most common problems people have when they learn python (probably because it's one of the few cases you actually have to declare something instead of it just implicitly working like you would expect as a beginner). I had to learn it the hard way (aka UnboundLocalError) too :)l4mpi– l4mpi01/03/2013 12:29:11Commented Jan 3, 2013 at 12:29
Like others have said, there doesn't seem to be any attempt to modify a string in your code, so that's hardly the problem.
That said, lastcall
looks random, should it perhaps be lastentry
?
I don't see any problem with your code (except for some details). String immutability does not seem to be a problem, here.
You may want to write, instead of the code in your question:
campdata = {'a1': 0, # Not "= 0"
'b2': 0}
and
campdata[lastentry] +=1 # not "lastcall"
Also, as l4mpi mentioned, you need a global lastentry
in the methods where it is modified.
Another point: relying on global variables is quite unusual. If at all possible, it would be best to use instance attributes (self.campdata
, self.lastentry
).