Importation brute et risquée¶
In [1]:
from modulex import *
In [2]:
try:
print(_x)
except NameError as e:
print("error : ", e)
_x = 2
print('_x = ', _x)
y = y + 3
print('y = ', y)
print()
try:
_f()
except NameError as e:
print("error : ", e)
g()
In [3]:
# de l'intérêt de ne PAS utiliser "import *"
def g():
print('surprise !')
In [4]:
g()
Importation de module: the right way¶
In [5]:
import modulex as m
print('m._x = ', m._x, 'm.y = ', m.y)
m.g()
m._f()
In [6]:
m.set_x(-5)
m.set_x(42)
print('x = ', m.get_x())
Différence module / main, test unitaire avec: if __name__ == '__main__':¶
In [7]:
print(__name__)
In [8]:
import math
print(math.__name__)
In [9]:
import modulex
print(modulex.__name__)