如何查看Python文件的汇编代码? [英] How to see the assembly code of a Python file?

查看:215
本文介绍了如何查看Python文件的汇编代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于好奇,我想看看与.py文件代码相对应的汇编指令。您可以提出任何值得信赖的解决方案吗?

Out of curiosity I would like to see the assembly instructions that correspond to the code of a .py file. Are there any trustworthy solutions you can propose?

推荐答案

dis 模块分解代码对象(使用 __ dict __ <从函数,类和对象中提取代码对象) / code>名称空间)。

The dis module disassembles code objects (extracting those from functions, classes and objects with a __dict__ namespace).

这意味着您可以使用它来分解整个模块:

This means you can use it to disassemble whole modules:

import dis

dis.dis(dis)

尽管这并不像您想象的那么有趣,因为大多数模块包含多个函数和类,从而导致大量输出。

although this isn't nearly as interesting as you may think as most modules contain several functions and classes, leading to a lot of output.

我通常关注具有我感兴趣的特定方面的较小功能;就像为链式比较生成的字节码一样:

I usually focus on smaller functions with specific aspects I am interested in; like what bytecode is generated for a chained comparison:

def f(x):
    return 1 < x ** 2 < 100

dis.dis(f)

这篇关于如何查看Python文件的汇编代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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