对这一计划的Python的内存模型 [英] Python memory model for this program

查看:223
本文介绍了对这一计划的Python的内存模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的查询是在下面的程序就被储存价值和功能的符号,当 HTTP跑:// pythontutor。 COM /

我的问题是:


  1. 如何蟒蛇执行模式查找上面的程序内存之前启动跨preting Python程序?我如何可视化的内存布局?例如,C可执行程序code /堆栈/堆/额外/数据段,只是作为一个例子,我不是在比较


  2. 是'常量'32/64位的存储区域存储与类型指定为整型值2的名称?


  3. 添加()/分()/其他功能如对象的列按图中,那么,如何看待我的函数中存储为对象?我如何可视化呢?


  4. 根据该图,是运指向函数sub函数指针()?



解决方案

  

每一个C(编译语言)程序有code /数据/堆栈/额外/堆执行之前在内存中加载段。难道蟒蛇间preTER创建任何内存布局为每个Python程序之前,就开始跨preting Python程序?如果是,我如何可视化的内存布局?


它有一种布局,但这里的堆是最重要的部分,因为每个对象都被放置到堆。在code段仅仅是间preTER,数据段是间preTER,以及内部状态,堆栈也是如此。

什么是相关的Python程序仅是堆。但布局本身就像任何其他程序。


  

常量 32/64位的存储区域存储分配为整型值 2 类型的名字吗?


有在当前的工作空间的名称,(这里:在模块的名字空间),它实质上是一个字典使字符串和任意对象之间分配。在这种情况下,它使字符串常量是指持有的价值 2 Integer对象。
(此对象可新建或再使用视情况而定;这没什么区别,因为它是不可变的)


  

添加() / 子() /其它功能如对象的列按图中,所以,我怎么看待功能被存储为对象?我怎么想象呢?


写在我的评论伊格纳西奥的回答是:

在功能的情况下,必须具有一定的字段,其中包含E中的对象。 G。在code以字节为code,它具有参数等数量方面它甚至有方法本身,例如 __获取__()这是内部调用的方法绑定到一个对象,或 __调用__()为真正的函数调用,除了 __格式__() __ __再版()等。

Integer对象了,在内心深处,用于存储实际值的地方。在的情况下,长()在PY2,或任何 INT()在PY3,它将数据存储到持有的值(如 2 )和以及需要为它的长度。此外,它有许多方法。看看的输出目录(2)来看看它有一堆的方法为好,如格式化,的为算术的,等等。


  

根据该图,是函数指针指向函数子()


样的,是的。

有一个函数对象,它的内部知道它最初的名字是。但是,这方面的知识仅用于显示的目的。

在你的情况,它是从两个名字所指,。因此参照其中一方有相同的结果。

请注意,有没有函数指针本身,也有只是参考,或名称,这是指任何类型的对象。类型的物体是固定的,但不是类型的引用(如不存在这样的东西)。

My query is on the below program with respect to symbols that are storing values and functions, when ran on http://pythontutor.com/.

My question is:

  1. How does python execution model look for above program on memory before start interpreting the python program? How do i visualise that memory layout? for example c executable has code/stack/heap/extra/data segments, just as an example, am not comparing

  2. Is 'const' a name of 32/64 bit memory area storing the value 2 with type assigned as integer?

  3. add()/sub()/other functions are shown in Objects column as per the diagram, So, How do i perceive functions being stored as Objects? How do i visualise it?

  4. As per the diagram, Is op a function pointer pointing to function sub()?

解决方案

Every C (compiled language) program has code/data/stack/extra/heap segments loaded in memory before execution. Does python interpreter create any memory layout for every python program before start interpreting the python program? If yes, How do i visualise that memory layout?

It has a kind of layout, but here the heap is the most important part, as every object is put to the heap. The code segment is merely the interpreter, the data segment is as well internal state of the interpreter, and the stack as well.

What is relevant for the Python program is only the heap. But the layout itself is like any other program.

Is const a name of 32/64 bit memory area storing the value 2 with type assigned as integer?

It is a name in the current working space, (here: in the module's namespace), which is essentially a dict which makes assignments between strings and arbitrary objects. In this case, it makes the string const refer to an integer object which holds the value 2. (This object can be created newly or re-used depending on the circumstances; this makes no difference, as it is immutable.)

add()/sub()/other functions are shown in Objects column as per the diagram, So, How do i perceive functions being stored as Objects? How do I visualise it?

As written in my comments to Ignacio's answer:

In the case of functions, you have an object which has certain fields, which contain e. g. the code in terms of bytecode, the number of parameters it has, etc. And it even has methods itself, for example __get__() which is called internally for binding a method to an object, or __call__() for the real function call, besides __format__(), __repr__() etc.

An integer object has, somewhere deep inside, a place for storing the actual value. In the case of a long() in Py2, or any int() in Py3, it stores the data to hold the value (e. g. 2) and as well the length needed for it. Besides, it has a number of methods. Have a look at the output of dir(2) to see it having a bunch of methods as well, such as for formatting, for arithmetics, etc.

As per the diagram, Is op a function pointer pointing to function sub()?

Kind of, yes.

There is a function object, which internally knows that its original name was sub. But this knowledge is only for displaying purposes.

In your case, it is referred from two names, op and sub. So referring to either of them has the same result.

Note that there are no "function pointers" per se, there are just references, or names, which refer to an object of any type. The type of an object is fixed, but not the "type of a reference" (as there is no such thing).

这篇关于对这一计划的Python的内存模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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