2020-10-19 21:08:55

This commit is contained in:
wizardforcel
2020-10-19 21:08:55 +08:00
parent 7f63048035
commit ab0caba1f0
140 changed files with 3982 additions and 3982 deletions

View File

@@ -4,7 +4,7 @@
对于 **C** 语言,代码一般要先编译,再执行。
```
```py
.c -> .exe
```
@@ -12,7 +12,7 @@
shell 脚本
```
```py
.sh -> interpreter
```
@@ -20,13 +20,13 @@ shell 脚本
**Python, Java** 等语言先将代码编译为 byte code不是机器码然后再处理
```
```py
.py -> .pyc -> interpreter
```
## eval 函数
```
```py
eval(statement, glob, local)
```
@@ -34,7 +34,7 @@ eval(statement, glob, local)
In [1]:
```
```py
a = 1
eval("a+1")
@@ -43,7 +43,7 @@ eval("a+1")
Out[1]:
```
```py
2
```
@@ -51,7 +51,7 @@ Out[1]:
In [2]:
```
```py
local = dict(a=2)
glob = {}
eval("a+1", glob, local)
@@ -60,7 +60,7 @@ eval("a+1", glob, local)
Out[2]:
```
```py
3
```
@@ -68,7 +68,7 @@ Out[2]:
## exec 函数
```
```py
exec(statement, glob, local)
```
@@ -76,7 +76,7 @@ exec(statement, glob, local)
In [3]:
```
```py
a = 1
exec("b = a+1")
@@ -85,14 +85,14 @@ print b
```
```
```py
2
```
In [4]:
```
```py
local = dict(a=2)
glob = {}
exec("b = a+1", glob, local)
@@ -101,7 +101,7 @@ print local
```
```
```py
{'a': 2, 'b': 3}
```
@@ -114,13 +114,13 @@ print local
## compile 函数生成 byte code
```
```py
compile(str, filename, mode)
```
In [5]:
```
```py
a = 1
c = compile("a+2", "", 'eval')
@@ -130,13 +130,13 @@ eval(c)
Out[5]:
```
```py
3
```
In [6]:
```
```py
a = 1
c = compile("b=a+2", "", 'exec')
@@ -147,7 +147,7 @@ b
Out[6]:
```
```py
3
```
@@ -155,14 +155,14 @@ Out[6]:
In [7]:
```
```py
import ast
```
In [8]:
```
```py
tree = ast.parse("a+2", "", "eval")
ast.dump(tree)
@@ -171,7 +171,7 @@ ast.dump(tree)
Out[8]:
```
```py
"Expression(body=BinOp(left=Name(id='a', ctx=Load()), op=Add(), right=Num(n=2)))"
```
@@ -179,7 +179,7 @@ Out[8]:
In [9]:
```
```py
tree.body.right.n = 3
ast.dump(tree)
@@ -188,13 +188,13 @@ ast.dump(tree)
Out[9]:
```
```py
"Expression(body=BinOp(left=Name(id='a', ctx=Load()), op=Add(), right=Num(n=3)))"
```
In [10]:
```
```py
a = 1
c = compile(tree, '', 'eval')
@@ -204,7 +204,7 @@ eval(c)
Out[10]:
```
```py
4
```
@@ -212,13 +212,13 @@ Out[10]:
In [11]:
```
```py
ast.literal_eval("[10.0, 2, True, 'foo']")
```
Out[11]:
```
```py
[10.0, 2, True, 'foo']
```