Created
April 12, 2011 14:30
-
-
Save smeghead/915593 to your computer and use it in GitHub Desktop.
スコープテスト
This file contains 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
% lua scope.lua | |
1 | |
2 | |
3 |
This file contains 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
% python scope.py | |
Traceback (most recent call last): | |
File "scope.py", line 9, in <module> | |
print(c()) | |
File "scope.py", line 4, in _counter | |
_count = _count + 1 | |
UnboundLocalError: local variable '_count' referenced before assignment |
This file contains 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
def counter(): | |
_count = 0 | |
def _counter(): | |
_count = _count + 1 | |
return _count | |
return _counter | |
c = counter() | |
print(c()) | |
print(c()) | |
print(c()) |
This file contains 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
function counter() | |
local _count = 0 | |
return function() | |
_count = _count + 1 | |
return _count | |
end | |
end | |
local c = counter() | |
print(c()) | |
print(c()) | |
print(c()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://morchin.sakura.ne.jp/effective_python/scope.html