Created
December 13, 2011 14:25
-
-
Save dketov/1472294 to your computer and use it in GitHub Desktop.
Область видимости
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- encoding: utf-8 -*- | |
| """ | |
| Глобальные и локальные переменные | |
| """ | |
| # global scope | |
| X = 99 # X and func assigned in module: global | |
| def func(Y): # Y and Z assigned in function: locals | |
| # local scope | |
| Z = X + Y # X is not assigned, so it's a global | |
| return Z | |
| func(1) # func in module: result=100 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- encoding: utf-8 -*- | |
| """ | |
| Скрытие имён переменных | |
| """ | |
| X = 88 # global X | |
| def func(): | |
| X = 99 # local X: hides global | |
| func() | |
| print X # prints 88: unchanged |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- encoding: utf-8 -*- | |
| """ | |
| Списки глобальных и локальных переменных | |
| """ | |
| def foo(arg): | |
| x = 1 | |
| print locals() | |
| locals()["x"] = 2 | |
| print "x=",x | |
| z = 7 | |
| print "z=",z | |
| foo(3) | |
| globals()["z"] = 8 | |
| print "z=",z |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- encoding: utf-8 -*- | |
| """ | |
| Доступ к глобальным переменным | |
| """ | |
| X = 88 # global X | |
| def func(): | |
| global X | |
| X = 99 # global X: outside def | |
| func() | |
| print X # prints 99 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment