了解Python如何“编译”或“解释”函数对象 [英] Understanding how Python "Compiles" or "Interprets" Function Objects

查看:123
本文介绍了了解Python如何“编译”或“解释”函数对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读以下文章,但我仍然无法确定某些内容。 $ b


  1. Python编译/解释过程


  2. 为什么python在解释之前将源代码编译成字节码?


如果我有一个包含以下代码的Python文件myfunctions.py。

< pre $ x = 3
def f():
print x
x = 2

然后,说 $ python myfunctions.py 运行得很好。
但现在对上述文件做一个小改动。新文件如下所示。

  x = 3 
def f():
print x
x = 2
f()#现在有一个函数调用

这次,代码给出了一个错误。现在,我正试图理解这种行为。到目前为止,这些都是我的结论。




  • Python为 x = 3

  • 它创建一个函数对象f,快速扫描并获得有关f范围内局部变量的字节码,但请注意Python中所有语句的字节码不太可能被构建。

  • 现在,Python遇到一个函数调用,它知道这个函数调用是合法的,因为只有关于函数对象f和它的局部变量的最小字节码存在。

  • 现在解释器负责执行字节码,但是从最初的脚印中知道x是一个局部变量,并且说 - 为什么在分配前打印?


有人可以对此发表评论吗?提前致谢。当解释器读取一个函数时,对于每个名称(变量)它遇到问题时,解释器会决定该名称是 local 还是非本地名称。使用的标准非常简单...在这个名称的正文的任何​​地方是否有一个赋值语句(禁止 global 语句)?例如:

  def foo():
x = 3#interpreter会将`x`标记为局部变量,因为我们在这里分配给它。

如果有对该名称的赋值语句,则该名称被标记为本地,否则,它被标记为非本地。



现在,就你的情况而言,你试图打印一个被标记为本地的变量,但是在你真正达到关键的赋值语句之前你要这样做。 Python寻找本地名称,但没有找到它,因此它引发了 UnboundLocalError



Python很动态的,并允许你做很多疯狂的事情,这是什么使它如此强大的一部分。这样做的缺点是,除非实际运行函数,否则很难检查这些错误 - 事实上,python已经决定不检查其他任何东西比语法直到函数运行。这就解释了为什么在你实际调用你的函数之前,你永远不会看到这个异常。




如果你希望Python将变量标记为全局变量,您可以使用显式的全局 c> 1 语句来执行此操作:

pre $



$ b $ $ b foo()#打印3
打印x#打印2

< sup> 1 python3.x更深入地介绍了 nonlocal 关键字


I have read the following posts but I am still unsure of something.

  1. Python Compilation/Interpretation Process

  2. Why python compile the source to bytecode before interpreting?

If I have a single Python file myfunctions.py containing the following code.

x  = 3
def f():
    print x
    x = 2

Then, saying $ python myfunctions.py runs perfectly fine. But now make one small change to the above file. The new file looks as shown below.

x  = 3
def f():
    print x
    x = 2
f() # there is a function call now

This time, the code gives out an error. Now, I am trying to understand this behavior. And so far, these are my conclusions.

  • Python creates bytecode for x=3
  • It creates a function object f, quickly scans and has bytecode which talks about the local variables within f's scope but note that the bytecode for all statements in Python are unlikely to have been constructed.
  • Now, Python encounters a function call, it knows this function call is legitimate because the bare minimum bytecode talking about the function object f and its local variables is present.
  • Now the interpreter takes the charge of executing the bytecode but from the initial footprint it knows x is a local variable here and says - "Why are you printing before you assign?"

Can someone please comment on this? Thanks in advance. And sorry if this has been addressed before.

解决方案

When the interpreter reads a function, for each "name" (variable) it encounters, the interpreter decides if that name is local or non-local. The criteria that is uses is pretty simple ... Is there an assignment statement anywhere in the body to that name (barring global statements)? e.g.:

def foo():
    x = 3  # interpreter will tag `x` as a local variable since we assign to it here.

If there is an assignment statement to that name, then the name is tagged as "local", otherwise, it gets tagged as non-local.

Now, in your case, you try to print a variable which was tagged as local, but you do so before you've actually reached the critical assignment statement. Python looks for a local name, but doesn't find it so it raises the UnboundLocalError.

Python is very dynamic and allows you to do lots of crazy things which is part of what makes it so powerful. The downside of this is that it becomes very difficult to check for these errors unless you actually run the function -- In fact, python has made the decision to not check anything other than syntax until the function is run. This explains why you never see the exception until you actually call your function.


If you want python to tag the variable as global, you can do so with an explicit global1 statement:

x = 3
def foo():
  global x
  print x
  x = 2

foo()  # prints 3
print x  # prints 2

1python3.x takes this concept even further an introduces the nonlocal keyword

这篇关于了解Python如何“编译”或“解释”函数对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆