sorry if this is an obvious question, but I am relatively new to python and am completely stumped at the moment. I am having difficulty getting my variables to work the way I want them too in a program I am working on.
Here is an example I quickly made to showcase my problem:
life = 200
def example(dudeHP):
dudeHP = dudeHP - 75
print (dudeHP)
example(life)
print (life) #output is 200, but I want it to be 125
As you can see, I am trying to get the value of the global variable "life" to change inside the def example codeblock with dudeHP (which would be a local variable if I am not mistaken?)
Is this viable? Is there a better way to accomplish this? Or am I doing it all wrong? Thankyou in advance :)
Well, first of all, you'll probably want to write an class or something to organize all this at some point. Modifying global variables from within functions or methods is usually considered a bad idea.
What you might want to try is this:
life = 200
def example(x):
return x - 75
life = example(life)
Of course in this case the operation of subtraction is so trivial that you don't need to encapsulate it in a function.
BTW, the question is not Python3 specific unless a newcomer gets stumped by the title. It is a general Python language question.
You have to understand the scope and the nature of the variables in Python. Variables are just names to hold certain values and their life is within the scope of the block they are defined it, although the inner scope variable can access the variable from the outer scope. The function you have defined has its own scope and it's own stack to maintain and you are passing a copy of the variable to that function.
The best way to deal with this is return value from the function as pointed out in another answer.
Having said that, if you really want to access variable in a global manner, you could do like this.
a = 200
def example(b):
global a
a = a - 25
example(a)
print a
But this is frowned upon and not a good style. There is also a trick which you are use, but that goes beyond the beginner level and it goes like this.
a = [200]
def example(a):
a[0] = a[0] - 25
example(a)
print a[0]
You can easily make mistakes you if adopt this way,
But the correct way to go about this is still:
a = 200
def example(b):
return b - 25
print example(a)
Further to Ned Batchelders and Kristoff's hints and suggestions to use a class, this is a way you could do that:
class Dude:
def __init__(self, hp=200):
self.hp = hp
def eat(self):
self.hp = self.hp + 25
def example(dude):
dude.hp -= 75
hero = Dude()
example(hero)
hero.eat()
print(hero.hp) # 150
Unless you have a good reason why example should be a stand-alone function, you should make it a method of Dude like eat, since example clearly acts on and in a sense belongs to an object of the class Dude.
In your function, the name dudeHP is local to the function. When you change it, you are only changing the local name, you cannot affect the value of the name passed in.
You can either make the integer be an element of a container that you pass in, so that you can change the container, or you can return the value from the function.
Since dudehp is having local scope in that function it doesn't have life outside that function. So, using dudehp to update life is illegal you can do this by using a global keyword or returing the value as others have mentioned... hope this explanation is useful and any corrections to this will be accepted.
Related
I've been trying to clean up my code for a neural network evolution simulator I'm working on, because it's grown to over 1000 lines and it's very difficult to read. Part of that process involves moving blocks of code from the main loop into their own functions (event handling, drawing to the screen, etc) or their own modules. However, I'm having trouble figuring out how to deal with the global variables.
As an example, I have a variable called "selected" that keeps track of a particular neural network creature selected by the user. I want to change the value of this variable in the event handling function when the user clicks on a new creature; I also want to change its value when the creature dies, which happens in a different block of code in its own function.
I understand that I can use the "global" keyword to do this, but my impression is that it's generally poor practice to do so. I also know that I can store these variables in a separate module and import them, but when I'm dealing with 30+ variables, typing "module_name.variable_name" every time I want to modify them seems unnecessarily tedious. I was also considering storing all of these values in a global dictionary so I can modify them from within functions, but again, typing "dict_name['var_name']" each time seems unnecessary.
So essentially my question is, what would be the best practice in this situation? Is it one of these solutions, or is there a cleaner approach? I'd like to make sure I'm on the right track before I spend hours overhauling my code. Thank you for your help!
30 global variables is just a big no-no. At some point you'll forget the global statement, and you'll create a local variable and will spend hours to debug your code.
you could build a singleton object instead
instead of:
a = 12
b = 33
c = "foo"
def run():
global a
if a == 34:
# do something
a += 1
you could create a big wrapping class with all variables as members:
class Program:
def __init__(self):
self.a = 12
self.b = 33
self.c = "foo"
now you can access all your variables with the self prefix. It may be tedious but at least it's short, and most good IDEs (pycharm, pyscripter, eclipse) propose completion (much better than a dictionary, where you cannot have completion)
# let's say it's the main method
def run(self):
if self.a == 34:
# do something
self.a += 1
now:
o = Program()
o.run()
print(o.a) # without __ prefix, your data is visible from the outside
So using an object not for inheritance, polymorphism or such, but just to define the "global" context and work inside the instance, with self. prefix, and say goodbye to global variables.
I'm quite new to programming and I keep reading everywhere "using methods to change global variables is not good coding. Use very little global variables and definitely don't change them". I can't seem to find a good explanation of why or what should be done.
Lets say I want to have some sort of a game. And it has variable health=100. Now I figured this should be a global variable and not inside a class/object, but that would also mean I would need to adjust it and so on by doing something like:
def minushealth():
global health
health -= 20
I can't really seem to figure this out and maybe there is just something simple I don't know here.
You could make health an explicit argument to and return value from the function:
def minus_health(health):
return health - 20
then assign the return value when you call it:
health = minus_health(health)
Even better, get rid of the "magic number" with an optional second argument:
def minus_health(health, amount=20):
return health - amount
Note that you can now test minus_health like:
assert minus_health(100) == 80
whereas if you implemented with global, you would have to do:
health = 100
minus_health()
assert health == 80
It might not seem like a huge problem in this simple example, but as your program gets more complex you have much more setup and tear-down to do, whereas a well-isolated function will still be effectively testable in one line.
jonrsharpe's answer is on point.
But for a game type answer you'd be better off using the class/attribute solution which would look like:
player_object.minus_health()
Where minus_health would look like:
class Player(object):
def __init__(self):
__health = 100
def minus_health(self):
self.__health -= 20
Of course this doesn't take into account if the health goes below 0, etc. But you should get the idea. This way allows all "Players" to have separate health attributes.
The Player class shown in another answer is probably going to be where you end up, but just an explanation of arguments to a function, you can only modify arguments that are mutable. In your specify case, an approach similar to the Player class, but simpler, is to manage your state in a dict and then pass that dict into the function to modify an attribute:
def minus_health(entity, amount=-20):
entity['health']+=amount
# Use like so
player = dict(name='John', health=100)
minus_health(player)
lists and objects are also mutable and will work in a similar way, i.e. their contents can be modified inside the function.
I often have the following code which either leads to variable shadowing or to a multiplication of local variables
def whenadult(age):
return 18 - age
age = 5
needtowait = whenadult(age)
age has the same logical role both when passed to the function as in the main code so I would like to avoid creating something like l_age in whenadult.
What is the pythonic way to solve the "shadowing vs. variable multiplication" dilemma?
UPDATE: following up on some comments I want to make it clear that I was looking for a Python best practice (as opposed to local vs. global variables scope)
The fact that the local variable (and function parameter) age happens to have the same name as a variable somewhere else in your program is irrelevant. The whole point of local variables is that they only live within the local scope of the function they're defined in.
The fact that the local variable has the same name as the variable used elsewhere as an argument is especially not a problem. In fact, it's very common in real-life code. For example, picking a random stdlib module, the 3.3 version of cmd, the Cmd.onecmd method has a variable named line, and it passes it as an argument to the self.default method, which binds it to a parameter that's also named line.
The fact that the variable used for the argument happens to be a global variable that you could have accessed, if you didn't have a local variable of the same name, is not a problem unless you actually wanted to access that global variable. Which you didn't want to in your existing code, and almost never should want to. In this case, and in most real-world cases, it's simply a coincidence that means nothing and affects nothing, not a problem you have to solve.
The problem you're having is that PyCharm can't guess whether you wanted the global age to be accessible in whenadult. Is it possible (if not in this trivial case, maybe in more complex cases) that a human might be similarly confused, slowing down his comprehension of your code? Or that you'll one day have to write code in some environment where your code reviewers or teacher or whatever will reject your code because it doesn't pass some linter with no warnings? Maybe.
But really, in any such environment, they'd probably complain about you using global variables in the first place. And you really don't need to here. The only reason age is a global is that it has to be accessible to the top-level code. If you move that code into a function, age can become a local in that function. For example:
def whenadult(age):
return 18 - age
def main():
age = 5
needtowait = whenadult(age)
main() # possibly with an if __name__ == '__main__' guard
This will make PyCharm happy, and any linter tools, and any easily-confused or rigidly-minded human readers. It'll even make your code a tiny bit faster. On the other hand, it's more code to read—only three lines and one indent, but then the whole program is only eight lines long. So, it's a tradeoff that you can make on a case-by-case basis.
Whenever I got the warning of shadowing variable in PyCharm. I would try to rename the local variable to use the underscore prefix as the convention. That's another way to consider in addition to wrap global variables into a main() function.
def whenadult(_age):
return 18 - _age
age = 5
needtowait = whenadult(age)
PyCharm is going out of its way to prevent you from accidentally accessing(usually due to typos) a variable from the outer scope because doing so can create really nasty bugs.
Short Answer:
Most executables don't need to access the global state, so follow this structure that makes main a python function:
def helper_function():
# a function that will be used by main()
.
.
.
def main():
# make MAIN a function.
helper_function()
if __name__ == '__main__':
main()
# DONT define any variables here in this GLOBAL scope
I'm currently developing some things in Python and I have a question about variables scope.
This is the code:
a = None
anything = False
if anything:
a = 1
else:
a = 2
print a # prints 2
If I remove the first line (a = None) the code still works as before. However in this case I'd be declaring the variable inside an "if" block, and regarding other languages like Java, that variable would only be visible inside the "if".
How exactly variable scoping works in Python and what's the good way to program in cases like this?
Thanks!
As a rule of thumb, scopes are created in three places:
File-scope - otherwise known as module scope
Class-scope - created inside class blocks
Function-scope - created inside def blocks
(There are a few exceptions to these.)
Assigning to a name reserves it in the scope namespace, marked as unbound until reaching the first assignment. So for a mental model, you are assigning values to names in a scope.
I believe that Python uses function scope for local variables. That is, in any given function, if you assign a value to a local variable, it will be available from that moment onwards within that function until it returns. Therefore, since both branches of your code are guaranteed to assign to a, there is no need to assign None to a initially.
Note that when you can also access variables declared in outer functions -- in other words, Python has closures.
def adder(first):
def add(second):
return first + second
return add
This defines a function called adder. When called with an argument first, it will return a function that adds whatever argument it receives to first and return that value. For instance:
add_two = adder(2)
add_three = adder(3)
add_two(4) # = 6
add_three(4) # = 7
However, although you can read the value from the outer function, you can't change it (unlike in many other languages). For instance, imagine trying to implement an accumulator. You might write code like so:
def accumulator():
total = 0
def add(number):
total += number
return total
return add
Unfortunately, trying to use this code results in an error message:
UnboundLocalError: local variable 'total' referenced before assignment
This is because the line total += number tries to change the value of total, which cannot be done in this way in Python.
There is no problem assigning the variable in the if block.
In this case it is being assigned on both branches, so you can see it will definitely be defined when you come to print it.
If one of the branches did not assign to a then a NameError exception would be raise when you try to print it after that branch
Python doesn't need variables to be declared initially, so you can declare and define at arbitrary points. And yes, the scope is function scope, so it will be visible outside the if.
i'm quite a beginner programmer, but for what i know, in python private variables don't exist. see private variables in the python documentation for a detailed discussion.
useful informations can also be found in the section "scopes and namespaces" on the same page.
personally, i write code like the one you posted pretty much every day, especially when the condition relies in getting input from the user, for example
if len(sys.argv)==2:
f = open(sys.argv[1], 'r')
else:
print ('provide input file')
i do declare variables before using them for structured types, for example i declare an empty list before appending its items within a loop.
hope it helps.
This question already has answers here:
Any way to modify locals dictionary?
(5 answers)
Closed 8 months ago.
Let's say I have this :
def a(dict):
locals().update(dict)
print size
def b():
size = 20
f(locals())
What do I have to do to access the size variable directly from the a function? I know of :
size = dict["size"]
but I think there should be a more direct way. I tried using locals().update(dict) but it didn't work. Is there a betteer way ?
The Python compiler optimizes access to local variables by recognizing at compile time whether the barenames a function is accessing are local (i.e., barenames assigned or otherwise bound within the function). So if you code:
def lv1(d):
locals().update(d)
print zap
the compiler "knows" that barename zap is NOT local (not assigned in function lv1) and so it compiles code to access it as a global instead -- whatever d contains won't matter.
If you prefer slow and bloated code, you can defeat the optimization by using an exec inside the function -- when the compiler sees the keyword exec, it KNOWS you're trying to make your code as slow, bloated and buggy as possible, and so it cooperates by not optimizing in any way, just about.
So, the following code works as you desire:
def lv1(d):
exec ""
locals().update(d)
print zap
lv1({'zap': 23})
it emits 23 as you want.
I hope it's clear from the above "deadpan humor" that the technique is not recommended, but I'd better spell it out very explicitly: for the dubious syntactic pleasure of writing print zap in lieu of print locals()['zap'], you ARE paying a hefty price in terms of performance. Still, like all sorts of dangerous tools that can be useful in rare use cases for really experienced guru-level programmers who really truly know what they're doing and why, exec is there, available for you to use (or mis-use) at your whim: Python does NOT stand in your way!-)
You can use closures to access the scope of another function:
def b():
def a():
print size
size = 20
a()
The trick is to stack functions which belongs together in each other. This way the inner function a can access the local variables of the outer function b.
But I don't know what you're really up to so I'm not sure if this helps.
Is this helping you somehow?
def print_size(size=None):
print(size)
dict = {'size': 'XXL'}
print_size(**dict)
print size
cannot work, because "size" is not known when the code is compiled. Your code will work if you change it like this:
def a(dict):
locals().update(dict)
print locals()["size"]
def b():
size = 20
a(locals())
But as the comment to your question already suggests: This is very strange code. I'm very sure that there are better solutions, if you explain what you really want to do.
I know, I know, the question is old and the accepted answer is already great.
Just adding two cents because it seems obvious but not mentioned: Make size local by assigning it a (default) value explicitly.
def a(dict):
size = 20 # size default value <- quick (and dirty?) fix
locals().update(dict)
print (size)
def b():
size = 20
a(locals())
So, the added line (size = 20) in a() makes size be an actual local variable without the needing for exec, being python3 compatible and without the optimization drawback.
And how could that be useful???
Well, with that we can implement something like def f(x, *args, y=10) in python2 as that raises a syntax error in python2 but just works in python3:
def f(x, *args, **kwargs):
y = 10
locals().update(kwargs)
print (x, y)
The example above works in python2 and python3