0

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?

asked Jan 3, 2013 at 12:06
3
  • 1
    I don't think you understand what "immutable" means. Commented 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? Commented Jan 3, 2013 at 12:09
  • 3
    This has nothing to do with mutability but with scope - global versus local variables. Commented Jan 3, 2013 at 12:09

3 Answers 3

8

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.

answered Jan 3, 2013 at 12:13
1
  • @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 :) Commented Jan 3, 2013 at 12:29
1

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?

answered Jan 3, 2013 at 12:09
1

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).

answered Jan 3, 2013 at 12:09

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.