Help understanding list operatoins inside functions in python 3

Laurent Pointal laurent.pointal at
Tue Jan 13 14:43:28 CET 2015


stephen.boulet at 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:

Or Dive Into Python

(and for your problem, its the same with Python2 and Python3)



More information about the Python-list mailing list