0

Python ans local variables

Hello As an exercise to python, I'm creating a "character generator" for a paper roleplaying game I own (Shadowrun).  This code involves a lot of manipulations on a few "characters attributes" which I define as variables in my code.  Very rapidly, I reach "UnboundLocalError" as my variables are modified in functions (and sub functions).  I get it, I need to declare them as global if I want to modify them inside a function. But it is getting insane. I'm writing global so many times! For "if statement", it seems I have to repeat the global declarations in every part of the if statement as the code jumps to the adequate if. Same when applying a new function to the same variable. So what is the proper way of modifying variables in a code when using multiple functions affecting the same variable?  https://code.sololearn.com/c17azYp5j0y5/?ref=app

20th Aug 2019, 12:25 PM
Olivier Makart
Olivier Makart - avatar
11 Respostas
+ 2
Do you know the difference between mutables and immutables? Class instances are mutable. So you can do something like this without having to use global: class A(): def __init__(self): self.x = 5 class B(): def __init__(self): self.x = 10 b=B() a = A() def incrX(race): race.x += 1 incrX(b) print(b.x)
20th Aug 2019, 3:43 PM
Thoq!
Thoq! - avatar
+ 1
Shadowrun rules! I also liked the novels. If I understand it right, you are not using classes (yet), which would make things easier. Then you have to declare a variable global at the beginning of the function. That should suffice. Or you could use the functions to return the new values which would mean that you would have to assign the variable again: Any_variable = foo() foo is the Function that returns the new value of Any_variable.
20th Aug 2019, 1:19 PM
Thoq!
Thoq! - avatar
+ 1
The idea is to have only very few functions and hopefully no variables outside of classes.
20th Aug 2019, 2:09 PM
Thoq!
Thoq! - avatar
+ 1
No problem. It was all for a good cause. I would love to play shadowrun again but I fear that ship has sailed for me.
21st Aug 2019, 1:54 PM
Thoq!
Thoq! - avatar
+ 1
I'm not sure I will play it again. But charterer creation is a good sandbox for training my python skills :)
21st Aug 2019, 1:55 PM
Olivier Makart
Olivier Makart - avatar
0
I thought "global" keyword only worked within a scope to define the variable out of that scope. It doesn't seem declaring global in the core of the code works. Or am I missing something?
20th Aug 2019, 12:58 PM
Olivier Makart
Olivier Makart - avatar
0
CBR, here is the code. Apparently when copying to Sololearn, I have some copy glitches, but you can see the idea. Multiple Connexions to variables, eventually reaching an error. edit: solved the copy glitch https://code.sololearn.com/c17azYp5j0y5/?ref=app
20th Aug 2019, 1:40 PM
Olivier Makart
Olivier Makart - avatar
0
Thoq!: I briefly tried with classes, but did not get any good results. Does declaring a variable within a class would help making it global in the functions outside the class and sub functions?
20th Aug 2019, 1:42 PM
Olivier Makart
Olivier Makart - avatar
0
Ok, so I need to rework my codes to have all functions inside one class? But is this going to solve the issue of having sub functions reaching only local definitions of the variable?
20th Aug 2019, 2:14 PM
Olivier Makart
Olivier Makart - avatar
0
This is still quite obscure to me. I'll play with your code to see what happens. Thanks for the time you took for me!
20th Aug 2019, 3:46 PM
Olivier Makart
Olivier Makart - avatar
0
Hi Thoq!, Thanks for the code. I played with it today and adapted my code to use classes. This now work smoothly and I don't have the problem of local variables. Plus I now understand a bit more the usage of classes! Thanks a lot!
21st Aug 2019, 8:31 AM
Olivier Makart
Olivier Makart - avatar