Variables locales et paramètres¶
In [1]:
def p1(pint):
vloc = 0
pint += 10
print(pint)
print(vloc)
In [2]:
vloc = 2
ret = p1(vloc)
vloc vaut 2 ou 0 ?
In [3]:
print("vloc = ", vloc)
print("ret = ", ret)
In [4]:
%load_ext tutormagic
In [5]:
%%tutor
def p1(pint):
vloc = 0
pint += 10
print(pint)
print(vloc)
vloc = 2
ret = p1(vloc)
Variables globales¶
In [6]:
vglob = 1
In [7]:
def p2():
try:
vglob = vglob + 10
except UnboundLocalError as e:
print('error: ', e)
print()
In [8]:
p2()
In [9]:
def p2a():
global vglob
vglob = vglob + 10
In [10]:
p2a()
In [11]:
print("vglob = ", vglob)
In [12]:
def p3():
print("vglob = ", vglob)
In [13]:
p3()
Pourquoi ici vglob est accessible sans le mot global ?