全局框架与堆栈框架 [英] global frame vs. stack frame

查看:78
本文介绍了全局框架与堆栈框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下所有内容均来自 www.pythontutor.com (一个很棒的工具和网站)的主页顺便说一句.)

Everything below is from the main page of www.pythontutor.com (a fantastic tool and website by the way).

这里是

在这里,作者将上述代码描述为当前执行点的全局框架"和堆栈框架":

Here's what the author describes as the "global frame" and the "stack frames" at the current point of execution for the above code:

我的问题:全局框架"和堆栈框架"有什么区别?这个术语是否正确(我在Google周围搜索并获得了各种不同的答案)?

My question: What's the difference between "global frame" and the "stack frame"? Is this terminology even correct (I googled around and got all kinds of different answers)?

推荐答案

TL DR:

调用函数时,将为本地执行创建框架.

TL DR:

when you call a function, a frame is created for the local execution.

加载模块时,将创建一个框架以用于全局模块执行.

When a module is loaded, a frame is created for the global module execution.

python导师不支持在多个文件中显示执行,因此主模块的初始框架似乎有些独特,但是您可以想象语句import foo为执行模块foo创建新框架就像调用函数会创建用于执行这些函数的框架一样.

python tutor doesn't support showing execution across multiple files so the initial frame for the main module can seem like something unique, but you could imagine the statement import foo creating a new frame for the execution of the module foo just like calling functions creates frames for executing those functions.

frames 是您实际使用的python对象可以互动:

frames are actual python objects that you can interact with:

import inspect

my_frame = inspect.currentframe()

print(my_frame) #<frame object at MEMORY_LOCATION>

print(my_frame.f_lineno) #this is line 7 so it prints 7
print(my_frame.f_code.co_filename) #filename of this code executing or '<pyshell#1>' etc.
print(my_frame.f_lineno) #this is line 9 so it prints 9

关于全局框架和局部框架没有什么特别的-它们只是执行的stack :

There is nothing particularly special about a global frame vs a local frame - they are just frames in the stack of execution:

Python 3.6.0a1 (v3.6.0a1:5896da372fb0, May 16 2016, 15:20:48) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import inspect
>>> import pprint
>>> def test():
...     pprint.pprint(inspect.stack())
... 
>>> test() #shows the frame in test() and global frame
[FrameInfo(frame=<frame object at 0x1003a3be0>, filename='<stdin>', lineno=2, function='test', code_context=None, index=None),
 FrameInfo(frame=<frame object at 0x101574048>, filename='<stdin>', lineno=1, function='<module>', code_context=None, index=None)]
>>> pprint.pprint(inspect.stack()) #only shows global frame
[FrameInfo(frame=<frame object at 0x1004296a8>, filename='<stdin>', lineno=1, function='<module>', code_context=None, index=None)]

每次调用函数(使用python源代码定义)时,都会在堆栈中添加一个用于其本地执行的框架,而一旦加载模块,则将用于全局执行该模块的框架添加到堆栈中.

When ever you call a function (defined with python source code) it will add a frame for it's local execution to the stack, when ever a module is loaded a frame for the global execution of the module is added to the stack.

框架没有任何标准化的命名约定,因此整个互联网上的术语可能会相互矛盾.通常,您可以通过文件和函数名称来识别它们. Python将全局框架称为名为<module>的函数,如上例(function='<module>')所示或错误:

Frames don't have any standardized naming convention, so terminology accross the internet will probably be contradicting. Usually you can identify them by the file and function name. Python refers to global frames as being a function named <module> as can be seen in above example (function='<module>') or in errors:

>>> raise TypeError
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    raise TypeError               # ^ up there
TypeError
                               


全局"与全局"之间唯一的真正区别是和功能"框架是全局框架,全局变量和局部变量之间没有区别:


The only real difference between "global" and "function" frames is that with global frames there is no distinction between global and local variables:

>>> my_frame.f_globals is my_frame.f_locals
True

这就是为什么将global关键字放在全局框架中毫无意义的原因,它指示变量名称-分配时应放在.f_globals中,而不是.f_locals中.但是除此之外,所有帧都几乎相等.

Which is why putting the global keyword in the global frame is meaningless, it indicates variable names that - when assigned - should be put in .f_globals instead of .f_locals. But other then that all frames are pretty much equal.

这篇关于全局框架与堆栈框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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