Пространства имён
# -*- encoding: utf-8 -*-
"""
Модуль simple
"""
#File: simple.py
print 'hello'
spam = 1
# -*- encoding: utf-8 -*-
"""
Модуль library
"""
#File: library.py
print 'starting to load...'
import sys
name = 42
def func(): pass
class klass: pass
print 'done loading.'
# -*- encoding: utf-8 -*-
"""
Модуль globs
"""
#File: globs.py
X = 88 # my X: global to this file only
def f():
global X # change my X
X = 99 # cannot see names in other modules
# -*- encoding: utf-8 -*-
"""
Модуль changer
"""
#File: changer.py
message = "First version"
def printer():
print message
# -*- encoding: utf-8 -*-
"""
Перезагрузка модуля
"""
import changer
changer.printer()
import changer
changer.printer() # no effect: uses loaded module
reload(changer) # forces new code to load/run
changer.printer() # runs the new version now
# -*- encoding: utf-8 -*-
"""
Атрибуты модуля и глобальные переменные
"""
X = 11 # my X: global to this file only
import globs # gain access to names in moda
globs.f() # sets globs.X, not my X
print X, globs.X
# -*- encoding: utf-8 -*-
"""
Изменение атрибутов модуля
"""
import simple # first import: loads and runs file's code
print simple.spam # assignment makes an attribute
simple.spam = 2 # change attribute in module
import simple # just fetches already-loaded module
print simple.spam # code wasn't rerun: attribute unchanged
# -*- encoding: utf-8 -*-
"""
Использование атрибутов, методов и классов модуля
"""
import library
library.sys
print library.name
print library.func, library.klass
print library.__dict__.keys()