May-12-2023, 03:34 PM
(May-12-2023, 05:58 AM)deanhystad Wrote: Not modify, assign.
In Python you create a variable when you do assignment. By default, the variable is created in the context where the assignment is made. If you use the global keyword, this tells Python to look in the global namespace/scope for the variable.
You do not need to use global when modifying a mutable object. For example:
x = [1] def modify(value): x[0] = value I don't really understand your code. What are the brackets for? It reminds me of a list or an array. modify(5) print(x)This works because I am not assigning a value to x, I am modifying the object that was already assigned to x.
Output:5