首页

/

归档

/

友链

/

Github

/

模拟面试

/

独立黑客

/

资料

/

订阅

/

RSS

/

关于我


再读 Python Language Reference

https://docs.python.org/3.6/reference/ 重读笔记。

Lexical analysis

>>> def foo():
...     print("hello")  # 1 tab here
...     print("world")  # 4 spaces here
  File "<stdin>", line 3
    print("world")
                 ^
IndentationError: unindent does not match any outer indentation level

Data model

Python中每个对象都有自己的id,类型和值。

Execution model

每个block都在一个 execution frame 里执行。

只要出现了上述的binding,Python就认为这个变量名所指向的变量在当前的block里,所以:

In [1]: foo = "hello"

In [2]: def print_foo():
   ...:     print(foo)
   ...:     foo = "world"
   ...:

In [3]: print_foo()
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-3-c80be14760b6> in <module>()
----> 1 print_foo()

<ipython-input-2-afc4cd9aff75> in print_foo()
      1 def print_foo():
----> 2     print(foo)
      3     foo = "world"
      4

UnboundLocalError: local variable 'foo' referenced before assignment

In [4]:

没看懂,来自:https://docs.python.org/3.6/reference/executionmodel.html

In [1]: class Foo:
   ...:     hello = "hello"
   ...:     def foo(self):
   ...:         print(hello)
   ...:

In [2]: Foo().foo()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-6c4f5adc4d1e> in <module>()
----> 1 Foo().foo()

<ipython-input-1-029db0d572c9> in foo(self)
      2     hello = "hello"
      3     def foo(self):
----> 4         print(hello)
      5

NameError: name 'hello' is not defined

In [3]: class Foo:
   ...:     a = 32
   ...:     b = [a + i for i in range(10)]
   ...:
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-395876c62295> in <module>()
----> 1 class Foo:
      2     a = 32
      3     b = [a + i for i in range(10)]
      4

<ipython-input-3-395876c62295> in Foo()
      1 class Foo:
      2     a = 32
----> 3     b = [a + i for i in range(10)]
      4

<ipython-input-3-395876c62295> in <listcomp>(.0)
      1 class Foo:
      2     a = 32
----> 3     b = [a + i for i in range(10)]
      4

NameError: name 'a' is not defined

In [4]:
In [1]: foo = "hello"

In [2]: def f():
   ...:     print(foo)
   ...:

In [3]: f()
hello

In [4]: foo = "world"

In [5]: f()
world

In [6]:

The import system

In [1]: import sys

In [2]: sys.modules['tornado']
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-2-36f3d84239fe> in <module>()
----> 1 sys.modules['tornado']

KeyError: 'tornado'

In [3]: sys.modules['tornado.web']
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-3-f1a34b2734fb> in <module>()
----> 1 sys.modules['tornado.web']

KeyError: 'tornado.web'

In [4]: import tornado.web

In [5]: tornado
Out[5]: <module 'tornado' from '/usr/local/lib/python3.5/dist-packages/tornado/__init__.py'>

In [6]: tornado.web
Out[6]: <module 'tornado.web' from '/usr/local/lib/python3.5/dist-packages/tornado/web.py'>

In [7]: sys.modules['tornado']
Out[7]: <module 'tornado' from '/usr/local/lib/python3.5/dist-packages/tornado/__init__.py'>

In [8]: sys.modules['tornado.web']
Out[8]: <module 'tornado.web' from '/usr/local/lib/python3.5/dist-packages/tornado/web.py'>

In [9]: