- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个 python 脚本。
def hello(self):
return 6
print hello()
在 CPython 中编译后反汇编
>>> c = compile(open('hello.py').read(), 'hello.py', 'exec')
>>> import dis
>>> dis.dis(c)
1 0 LOAD_CONST 0 (<code object hello at 0x1006c9230, file "hello.py", line 1>)
3 MAKE_FUNCTION 0
6 STORE_NAME 0 (hello)
3 9 LOAD_NAME 0 (hello)
12 CALL_FUNCTION 0
15 PRINT_ITEM
16 PRINT_NEWLINE
17 LOAD_CONST 1 (None)
20 RETURN_VALUE
我很好奇 <code object hello at 0x1006c9230 ...>
是怎么来的存储在 CPython 代码对象中。有 co_code
函数,但只打印出字节码指令。如果我序列化我得到的 CPython 代码对象
>>> import marshal
>>> marshal.dumps(c)
'c\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00@\x00\x00\x00s\x15\x00\x00\x00d\x00\x00\x84\x00\x00Z\x00\x00e\x00\x00\x83\x00\x00GHd\x01\x00S(\x02\x00\x00\x00c\x01\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00C\x00\x00\x00s\n\x00\x00\x00d\x01\x00}\x01\x00|\x01\x00S(\x02\x00\x00\x00Ni\x06\x00\x00\x00(\x00\x00\x00\x00(\x02\x00\x00\x00t\x04\x00\x00\x00selft\x01\x00\x00\x00x(\x00\x00\x00\x00(\x00\x00\x00\x00s\x08\x00\x00\x00hello.pyt\x05\x00\x00\x00hello\x01\x00\x00\x00s\x04\x00\x00\x00\x00\x01\x06\x01N(\x01\x00\x00\x00R\x02\x00\x00\x00(\x00\x00\x00\x00(\x00\x00\x00\x00(\x00\x00\x00\x00s\x08\x00\x00\x00hello.pyt\x08\x00\x00\x00<module>\x01\x00\x00\x00s\x02\x00\x00\x00\t\x03'
我知道
def hello(self):
return 6
存储在转储中的某处,因为如果我将其更改为 return 5
,转储中的一个字节从 6 切换到 5。
1) 有没有一种方法可以从 CPython 代码对象访问函数体。我能拿到的最近的c.names
但这只会打印出一个字符串。我假设在幕后它是一个被序列化为字符串的 PyObject。我还想确认函数体确实存储在 c.names
中.
2) marshal dump 将函数存储为字节码指令还是未编译的文字?当我搜索操作码\x83 (RETURN_VALUE) 时,我倾向于未编译的文字,它只在转储中出现一次。我相信这意味着只有一个 return 语句,而应该有两个:一次退出函数 hello,一次返回 None 以退出脚本。
版本
Python 2.7.13+ (heads/2.7:96f5020597, May 26 2017, 15:26:13)
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
最佳答案
让我们分解一下。
首先,让我澄清一下 CPython 究竟是如何存储函数的。解析函数时,CPython 将函数的数据存储在代码对象 中。 CPython 使用代码对象来存储函数、类和模块。然后将表示函数的代码对象序列化为特定的字节代码格式。
函数的代码对象存储在它们的__code__
中。属性:
>>> def foo():
pass
>>>
>>> foo.__code__
<code object foo at 0x7f8bd86ce5d0, file "<pyshell#14>", line 1>
>>>
这些代码对象包含与函数相关的各种数据,例如函数参数、引用的常量(例如 1
或 "Hello"
)和名称。函数的字节码存储在.co_code
中属性。这是 CPython 运行您的函数时实际执行的内容:
>>> def foo():
pass
>>> foo.__code__.co_code
b'd\x00\x00S' # bytecode for foo
>>>
现在您了解了 CPython 的基本功能,我们可以解决您的具体问题。
Is there a way I can access the function body from the CPython code object. The closest I can get it c.names but that only prints out a string. I'm assuming there behind the scenes it is a PyObject that is being serialized as a string. I would also like a confirmation that the function body is indeed stored in c.names.
函数体没有存储在co_name
中代码对象的属性。它存储在 .co_code
中属性如上所述。您的其他假设也有点偏离。从技术上讲,由于 Python 中的所有对象都“继承”自 PyObject
, 说函数体被序列化为 PyObject
是正确的序列化为字符串。但是,最好说它被序列化为PyStringObject
。这是表示字符串的特定类型。
Does marshal dump store the function as bytecode instructions or as a uncompiled literal? I'm leaning toward uncompiled literal as I searched for the opcode \x83 (RETURN_VALUE) and it only appears once in the dump. I believe this implies that there is only one return statement when there should be two: once to exit out of the function hello and once to return None for exiting the script.
两者都不做。 marhsal.dumps()
接受一个代码对象,将整个代码对象序列化为 CPython 特定格式,并返回一个表示序列化代码对象的字节对象。但是,您的第二个陈述是正确的。在每个 Python 脚本的末尾,隐含 None
被退回。这可以通过将空参数传递给 dis.dis()
来观察。 :
>>> import dis
>>> dis.dis("")
1 0 LOAD_CONST 0 (None)
3 RETURN_VALUE
>>>
I know for a fact that
<code object hello at 0x1006c9230 ...>
is not stored in the co_code attribute of the original c. This is because no matter how I change the inside of def hello() the same disassembler output is given. To be clear this is a function inside a function/script not just a function as you gave in your example.
在您的具体示例中,变量 c
是一个代码对象,代表模块——而不是函数——“hello.py”。你的权利,函数的代码对象hello()
不在 co_code
中.它存储在模块的代码对象的 co_consts
中。属性:
>>> co = compile(open('hello.py').read(), 'hello.py', 'exec')
>>> co.co_consts
(<code object hello at 0x7fedcbd3dc00, file "hello.py", line 1>, 'hello', None)
>>>
这是因为 Python 执行代码的方式。常量不直接存储在代码对象的字节码中。相反,它们存储在自己单独的元组中。每当在函数代码中引用常量时,实际常量存储在 co_consts
中和一个索引,它对应于所述常量在co_consts
中的位置放在字节码中。
为什么你的反汇编器输出 hello()
的原因的代码对象永远不会改变是因为所有 dis.dis()
正在做的只是显示 hello()
的字符串表示代码对象。 hello()
的代码对象确实在您更改代码时发生了变化,但该变化由 dis
显示.它不显示 hello()
的实际更改 属性s 代码对象。
关于python - 名称和函数体如何存储在 Python 代码对象中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44212297/
我正在尝试做这样的事情:Name[i] = "Name"+ (i+1) 在 forloop 中,这样数组的值将是:Name[0] = Name1,Name[1] = Name2,Name[2] = N
我读了here,在GSP中我们可以这样写: ${params.action} 从GSP中,我们可以使用${params.action}作为参数调用Javascript函数(请参阅here)。 是否有其
我的问题:非常具体。我正在尝试想出解析以下文本的最简单方法: ^^domain=domain_value^^version=version_value^^account_type=account_ty
我创建了一条与此类似的路线: Router::connect("/backend/:controller/:action/*"); 现在我想将符合此模式的每个 Controller 路由重命名为类似
我在 Visual Studio 2013 项目中收到以下警告: SQL71502 - Procedure has an unresolved reference to object 最佳答案 这可以
任何人都可以指导我使用名称/值 .NET 集合或 .NET 名称/值字典以获得最佳性能吗?请问最好的方法是什么?我的应用程序是 ASP.NET、WCF/WF Web 应用程序。每个集合应该有 10 到
我在 Zend Framework 2 中有一个默认模块: namespace Application\Controller; use Zend\Mvc\Controller\AbstractActi
这是表格: 关于javascript - 在 javascript 中,这是一个有效的结构吗? : document. 名称.名称.值?,我们在Stack Overflow上找到一个类似的
HtmlHelper.ActionLink(htmlhelper,string linktext,string action) 如何找出正确的路线? 如果我有这个=> HtmlHelper.Actio
我需要一些有关如何将 Controller 定义传递给嵌套在 outer 指令中的 inner 指令的帮助。请参阅http://plnkr.co/edit/Om2vKdvEty9euGXJ5qan一个
请提出一个数据结构来表示内存中的记录列表。每条记录由以下部分组成: 用户名 积分 排名(基于积分)- 可选字段- 可以存储在记录中或可以动态计算 数据结构应该支持高效实现以下操作: Insert(re
错误 : 联合只能在具有兼容列类型的表上执行。 结构(层:字符串,skyward_number:字符串,skyward_points:字符串)<> 结构(skyward_number:字符串,层:字符
我想要一个包含可变数量函数的函数,但我希望在实际使用它们之前不要对它们求值。我可以使用 () => type 语法,但我更愿意使用 => type 语法,因为它似乎是为延迟评估而定制的。 当我尝试这样
我正在编写一个 elisp 函数,它将给定键永久绑定(bind)到当前主要模式的键盘映射中的给定命令。例如, (define-key python-mode-map [C-f1] 'pytho
卡在R中的错误上。 Error in names(x) <- value : 'names' attribute must be the same length as the ve
我有字符串,其中包含名称,有时在字符串中包含用户名,后跟日期时间戳: GN1RLWFH0546-2020-04-10-18-09-52-563945.txt JOHN-DOE-2020-04-10-1
有人知道为什么我会收到此错误吗?这显示将我的项目升级到新版本的Unity3d之后。 Error CS0103: The name `Array' does not exist in the curre
由于 Embarcadero 的 NNTP 服务器从昨天开始就停止响应,我想我可以在这里问:我使用非数据库感知网格,我需要循环遍历数据集以提取列数、它们的名称、数量行数以及每行中每个字段的值。 我知道
在构建Android应用程序的子项目中,我试图根据根build.gradle中的变量设置版本代码/名称。 子项目build.gradle: apply plugin: 'com.android.app
示例用例: 我有一个带有属性“myProperty”的对象,具有 getter 和 setter(自 EcmaScript 5 起支持“Property Getters 和 Setters”:http
我是一名优秀的程序员,十分优秀!