Help understanding list operatoins inside functions in python 3
Laurent Pointal
laurent.pointal at laposte.net
Tue Jan 13 08:43:28 EST 2015
Hello,
stephen.boulet at gmail.com wrote:
> I'm a bit confused why in the second case x is not [1,2,3]:
>
> x = []
>
> def y():
> x.append(1)
>
> def z():
> x = [1,2,3]
Here x is a local, so global x is not modified.
If you want to modify gobal x, write:
def z():
global x
x = [1,2,3]
You can read the Python FAQ about global/local rules:
https://docs.python.org/3/faq/programming.html#what-are-the-rules-for-local-and-global-variables-in-python
Or Dive Into Python
http://www.diveintopython.net/html_processing/locals_and_globals.html
(and for your problem, its the same with Python2 and Python3)
Or…
A+
Laurent.
More information about the Python-list
mailing list