開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python (プログラミング言語)
Learning Python (Mark Lutz (著)、Oreilly & Associates Inc)のPART III.(Functions and Generators)、CHAPTER 17(Scopes)、Test Your Knowledge: Quizを解いてみる。
その他参考書籍
Test Your Knowledge: Quiz
入出力結果(Terminal)
$ python3 Python 3.3.4 (default, Feb 10 2014, 22:06:52) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> x = 'spam' >>> def f():x='NI!' ... >>> f() >>> x # spam 'spam' >>> quit() bash-4.2$ python3 Python 3.3.4 (default, Feb 10 2014, 22:06:52) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> x='spam' >>> def f():print(x) ... >>> f() # spam spam >>> x 'spam' >>> def f():x='ni!' ... >>> f() >>> print(x) # spam spam >>> x 'spam' >>> def f():x='ni';print(x) ... >>> f() # ni ni >>> x # spam 'spam' >>> def f():global x;x='ni' ... >>> f() >>> x # ni 'ni' >>> x='spam' >>> x 'spam' >>> def f(): ... x = 'ni' ... def nested(): ... print(x) ... nested() ... >>> f() # ni ni >>> def f(): ... x = 'ni' ... def nested(): ... nonlocal x ... x = 'spam' ... nested() ... print(x) ... >>> f() # spam spam >>> def f(): ... x = 10 ... def g(): ... nonlocal x ... x += 1 ... print(x) ... return g ... >>> a = f() >>> a() 11 >>> a() 12 >>> a() 13 >>> a() 14 >>> a() 15 >>> $
0 コメント:
コメントを投稿