简体   繁体   English

当我们说 print("hello") 时,在 python 中。 是否会创建带有 hello 的 object 并将其存储在堆上然后打印? 或者它只是打印并且没有 object 是

[英]in python when we say print("hello"). Does an object with hello is created and stored on heap and then printed? or it just print and no object is ther

I am new to python.我是 python 的新手。 In python when we say print("hello").在 python 中,当我们说 print("hello") 时。 Does an object with hello is created and stored on heap and then printed?是否会创建带有 hello 的 object 并将其存储在堆上然后打印? or it just print and no object is created on heap.或者它只是打印并且没有在堆上创建 object。

You can try to disassemble it and you'll see.你可以尝试拆开它,你会看到。 Python is an interpreted language with its interpreter being a virtual machine (think assembly compiled to machine instructions, but a bit abstracted). Python 是一种解释语言,其解释器是虚拟机(认为汇编编译为机器指令,但有点抽象)。

For disassembling Python provides an easy dis.dis() method对于拆卸 Python 提供了一个简单的dis.dis()方法

import dis

def hello():
    print("hello")

dis.dis(hello)

That it creates the value (I'd guess while parsing, but don't quote me on that. I'd need to check the CPython's code for that) as a constant and then loads it.它创建的值(我会在解析时猜测,但不要引用我的话。我需要检查 CPython 的代码)作为常量,然后加载它。

Now where it's created is dependent on the underlying Python language interpreter, in this case CPython (and PyPy is another one).现在它的创建位置取决于底层的 Python 语言解释器,在这种情况下是 CPython(而 PyPy 是另一个)。 For CPython it'd be this:对于 CPython,它将是这样的:

Memory management in Python involves a private heap containing all Python objects and data structures. Memory 管理 Python 涉及一个包含所有 Python 对象和数据结构的私有堆。 ( ref ) 参考

But most likely yes, it'll be on heap due to "hello" string being a non-C or non-simple data storage.但很可能是的,由于"hello"字符串是非 C 或非简单数据存储,它将在堆上。 Or better said a Python object ( PyObject ), which is in general thrown on heap with Python's malloc or so called PyMalloc or other malloc-like implementation the interpreter chooses to use either by compile-time options or library detection on the runtime. Or better said a Python object ( PyObject ), which is in general thrown on heap with Python's malloc or so called PyMalloc or other malloc-like implementation the interpreter chooses to use either by compile-time options or library detection on the runtime.

  4           0 LOAD_GLOBAL              0 (print)
              2 LOAD_CONST               1 ('hello')
              4 CALL_FUNCTION            1
              6 POP_TOP
              8 LOAD_CONST               0 (None)
             10 RETURN_VALUE

However, as noted in the comments, and as with every modern compiler (so even Python's) the code can be optimized, therefore it can be even thrown into a raw C data storage (eg *char[] or similar) and then only referenced via the Python's object.但是,正如评论中所指出的,并且与每个现代编译器(甚至是 Python)一样,可以优化代码,因此甚至可以将其放入原始 C 数据存储(例如*char[]或类似的)中,然后仅引用通过 Python 的 object。

A bit simpler disassembling with just the code you have.仅使用您拥有的代码进行更简单的反汇编。 dis.dis() accesses the code block in the hello() function, so the result is the same, just the line on the left isn't 4 but a 1 dis.dis()访问hello() function 中的代码块,所以结果是一样的,只是左边那行不是4而是1

$ cat main.py;echo "---";python -m dis main.py
print("hello")
---
  1           0 LOAD_NAME                0 (print)
              2 LOAD_CONST               0 ('hello')
              4 CALL_FUNCTION            1
              6 POP_TOP
              8 LOAD_CONST               1 (None)
             10 RETURN_VALUE

Related:有关的:

I believe it creates a str object which has the method str (), which print() calls, then it gets garbage collected afterwards, but I may be wrong我相信它会创建一个 str object,它有方法str (),print() 调用,然后它会被垃圾收集,但我可能错了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM