Use a global statement, like so:
COUNT = 0
def increment():
global COUNT
COUNT = COUNT+1
increment()
Global variables can be accessed without using global, but the statement is required in order to change the value of the global variable.
Use a global statement, like so:
COUNT = 0
def increment():
global COUNT
COUNT = COUNT+1
increment()
Global variables can be accessed without using global, but the statement is required in order to change the value of the global variable.
This is because globals don't bleed into the scope of the function. Use the global statement to force this for assignment:
>>> COUNT = 0
>>> def increment():
... global COUNT
... COUNT += 1
...
>>> increment()
>>> print(COUNT)
1
Note that using globals is a really bad idea - it makes code hard to read, and hard to use. Instead, return a value from your function (using return) and use that to do something. If the same data needs to be accessible from a range of functions, consider making a class.
It's also worth noting that CAPITALS is generally reserved for constants, so it's a bad idea to name global variables like this. For normal variables, lowercase_with_underscores is preferred.
So going thorough scoping rules I learned that inside a function if you used a global variable and then assign it a value it will raise UnboundLocalError.
x = 7
def f():
x = 7 #I will now create new variable x inside f's local scopeCorrect above one is fine.
x = 7
def f():
print(x) #x is not defined already, okay I will use global x nowOkay this is also fine.
But,
x = 7
def f():
print(x) #x is not defined already, okay I will use global x now
x = 7 #I will now create new variable x inside f's local scope (OHH, scope clash raise UnboudLocalError)Fine, might is right.
But why you are not allowing me to do this.
x = 7
def f():
x += 1In above I am just incrementing x, why don't it uses x from global scope?
So, from my understanding I think it due to += used here the statement can be broken into:
x = x + 1 # which in turn is # x = x.__add__(1)
Why didn't they implemented it like:
1. Evaluate expression after =, i.e., x.__add__(1) 2. Now we encountered x, take it from global. 3. In function scope we have x from global so when evaluating left expression x = [...] we assign it value from RHS.
Videos
So, I'm trying to code a simple dice-throw based text game. It's kind of like choose-your-own-adventure with random choices.
I want to keep track of how many times the die have been thrown, but I can't seem to figure out how to do so.
Here is the code for the function:
def winner():
roll1 = throw()
print P1," rolls a ",roll1
roll2 = throw()
print P2," rolls a ",roll2
if roll1 > roll2:
print P1," wins with a ",roll1,"!"
return P1
elif roll2 > roll2:
print P2," wins with a ",roll2,"!"
return P2
else:
print "It's a tie, throw again."
winner()
global turn
turn = 0
turn += 1 I know that the last three lines are not right, but I don't know what the right way to have turn as a global variable in the script that increments each time winner() completes is. Especially because I don't want to count ties as extra turns.
As others suggested, itertools.count() is the best option, e.g.
import itertools
global_counter1 = itertools.count()
global_counter2 = itertools.count()
# etc.
And then, when you need it, simply call next:
def some_func():
next_id = next(global_counter1)
EDIT: Changed global_counter1.next() (which worked only in Python 2) to next(global_counter1), which works also in Python 3.
You can create a generator that has an infinite loop. Each call of next(generator) will return a next value, without limit. See What does the "yield" keyword do in Python?
def create_generator()
i=0
while True:
i+=1
yield i
generator = create_generator()
print(next(generator))
print(next(generator))
Try this:
def operate():
number = input("Enter a number to start: ")
while True:
readline("data.csv", number)
op = raw_input("Repeat (R), Next (N), Previous (P), or Quit (Q) ")
if op == "R": pass
elif op == "N": number += 1
elif op == "P": number -= 1
elif op == "Q": break
else: raise Exception("Incorrect command")
operate()
This keeps it local, no need for globals, and it puts it into a loop which should reduce overhead. I also added a Quit option.
Haven't tried it, but why not pass it as an argument?
Like this:
def start():
numberstr = raw_input("Enter a number to start ")
number = int(numberstr)
readline("data.csv", number)
control(number)
def control(number):
operation = raw_input("Repeat (R), Next (N), or Previous (P) ")
if operation == "R":
readline("data.csv", number)
control(number)
elif operation == "N":
number +=1
readline("data.csv", number)
control(number)
elif operation == "P":
number -=1
readline("data.csv", number)
control(number)
else:
print "Incorrect command"
control(number)
start()
Hope this helps!
You have two Fr variables in different scopes
Fr = 0
Is outside of your function, thus never changed.
Fr += 1
Is inside a function and will be incremented, but this is a different variable.
Here is the solution (one of the possible ones):
def func(Ie, Vm, Fr):
for i, t in enumerate(time):
if i == 0:
Vm[i] = -70
else:
Vm[i] = Vm[i-1] + (El- Vm[i-1] + Ie*Rm) / tau_m * dt
if Vm[i] >= Vth:
Fr += 1
Vm[i] = El
return Fr
Then, just do
Fr = func(Ie, Vm, Fr)
One more tip.
If your Fr variable is always 0 by default you can do this:
def func(Ie, Vm, Fr=0):
when defining the function, and pass the third paramenter only when you need something different that 0.
If you want to modify variable outside of the scope of the function you need to use the global keyword
my_var = True # Declare a variable on the global scope
def my_function():
global my_var # tell the interpreter that you want to use the global "my_var"
my_var = False # Change global my_var value
my_function() # call the function
print my_var # check result
Be advised however that it is not considered a good practice to do so.
You should try to isolate as much as you can the scopes in your code to make it more readable.
my_var = 3 # Declare a variable on the global scope
def my_function(my_var):
return my_var + 1
my_var = my_function(my_var) # call the function and assign result to global variable
print my_var # check result
You need:
counter = 0
def addCounter():
global counter
counter = counter + 1
return counter
Explanation: in Python, the declaration of inner variables is implicit, an assignment automatically declares the values on the left-hand side, however that declaration is always in the local scope. That's why if you write this:
counter = 0
def addCounter():
return counter
it will work fine but as soon as you add an assignment
counter = 0
def addCounter():
counter += 1
return counter
it breaks: the assigment adds an implicit local declaration. global overrides this, although it requires that the global exist beforehand, it does not create a global, it just tells the function that this is a global variable it can reassign to.
I've tried passing the counter variable in as a parameter as well, but that doesn't work either.
Indeed not. Python's evaluation strategy is sometimes called "pass by sharing" (or "pass reference by value") which is technically "pass by value" but that term gets a bit confusing as the values in this case are references, the references are copied but the objects referred to are not, and thus the end-behaviour diverges from the normal expectations of "pass by value" expectations.
Using a class rather than global:
Another way to handle (not use) global variables is to wrap the functions and variables you wish to be global in a class.
While this is a little heavy for this specific case - classes add a host of functionality and flexability to the project. (Personally) highly recommended.
For example:
class Processor():
"""Class container for processing stuff."""
_counter = 0
def addcounter(self):
"""Increment the counter."""
# Some code here ...
self._counter += 1
# See the counter incrementing.
proc = Processor()
proc.addcounter()
print(proc._counter)
proc.addcounter()
print(proc._counter)
Output:
1
2
I have a counter set up to notify me how many times I write to a file: amount_of_times = 1. However every-time I write to the file it is not incrementing, is there a way for me to get it to increment, so I know how many times I ran the program?
with open ('TextInfo.txt','w') as filer:
amount_of_times = 1
filer.write('I wrote one time')
filer.write('Again')
print('Executed amont of times: {}'.format(amount_of_times))
amount_of_times = amount_of_times + 1