@@ -132,69 +132,4 @@ def get_files(dir_path):
132132``` python showLineNumbers
133133dir = os.curdir
134134get_files(dir )
135- ```
136-
137- ## Byte Code 编译
138-
139- Python, Java 等语言先将代码编译为 byte code(不是机器码),然后再处理:
140-
141- > .py -> .pyc -> interpreter
142-
143- eval(statement, glob, local)
144-
145- 使用 eval 函数动态执行代码,返回执行的值。
146-
147- exec(statement, glob, local)
148-
149- 使用 exec 可以添加修改原有的变量:
150-
151- ``` python showLineNumbers
152- a = 1
153- exec (' b = a + 10' )
154- print (b)
155- ```
156-
157- ``` python showLineNumbers
158- local = dict (a = 2 )
159- glob = {}
160- exec (" b = a+1" , glob, local)
161-
162- print (local)
163- ```
164-
165- compile 函数生成 byte code:
166- compile(str, filename, mode)
167-
168- ``` python showLineNumbers
169- a = 1
170- b = compile (' a+2' , ' ' , ' eval' )
171- print (eval (b))
172- ```
173-
174- ``` python showLineNumbers
175- a = 1
176- c = compile (" b=a+4" , " " , ' exec' )
177- exec (c)
178- print (b)
179- ```
180-
181- ``` python showLineNumbers
182- # abstract syntax trees
183- import ast
184-
185- tree = ast.parse(' a+10' , ' ' , ' eval' )
186- ast.dump(tree)
187- ```
188-
189- ``` python showLineNumbers
190- a = 1
191- c = compile (tree, ' ' , ' eval' )
192- d = eval (c)
193- print (d)
194- ```
195-
196- ``` python showLineNumbers
197- # 安全的使用方法 literal_eval ,只支持基本值的操作:
198- b = ast.literal_eval(' [10.0, 2, True, "foo"]' )
199- print (b)
200- ```
135+ ```
0 commit comments