Python:字面上“打印”一个功能 [英] Python: literally "printing" a function

查看:160
本文介绍了Python:字面上“打印”一个功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我不会进入,我需要一些方法来打印一个函数。



我知道当你在一个函数对象上运行打印时,你会得到一些复杂< 010101045函数>输出。所以为了我的理解,也许唯一的办法是运行某种类型的python记事本模块。然后我可以设计一个算法来编写一行文本,以匹配每个函数行,以获得打印的函数。



如果有人知道其他方法来实现这一点,请告诉我,因为我确信这样做是非常复杂的。如果这是唯一的办法,如果有人可以走过我的基本步骤,这样做也将不胜感激。



我也发现,有趣的是能够将打印功能的语法保存为python格式,以便我可以直接将其直接导入到程序中,如果我想要的。是的,我意识到这变得非常复杂,但是如果有人知道如何做到这一点,那么一般步行将是非常好的。



如果有人得到了将上述过程自动化到程序可以从导入程序的内部修改导入的模块,现在真的很酷。



(在理想的情况下,修改后的版本当然会以不同的名称保存,并被导入以及以前的版本,并在程序中的那一点处理。导入)



我想在这一点上,我只是想知道从python程序导入,保存和修改文件的基本命令。



所有的见解都表示赞赏,



谢谢,



:如果你想知道为什么我想做所有这一切,这应该是显而易见的,如果不是需要一段时间来解释。

解决方案

使用 检查。获取资源

  def a():
pass

import inspection
print inspect.getsource(a)

当然,这只有在您访问原始来源时才有效。 p>

如果你没有原始来源,你可以得到的最多的是字节代码(见 dis.dis



编辑:Python交互式shell是一个例子,其中 inspect 无法访问源。 / p>

For reasons I won't get into, I need some way to literally print a function.

I know when you run print on a function object you get some complicated <010101045 function> output. So to my understanding perhaps the only way to do this is run some kind of python notepad module. Then I could devise an algorithm to write a line of text to match each function line so as to get a printed function.

If anyone knows some other way to achieve this, please do tell because I am sure doing it this way is incredibly complicated. If however this is the only way, if someone could walk me through the basic steps to doing so that would also be greatly appreciated.

It occurred to me also that it would be interesting to be able to have the syntax of the printed function be saved in python format so that I could literally just import it directly into a program if I wanted. Yes, I realize this is getting really complicated, but again, a general walk through would be really cool if anyone knows how to do this.

If anyone has gotten as far as automating the aforementioned process to the point where a program could modify an imported module from inside of the program importing it, now that would really cool.

(In an ideal case of course the modified version would be saved under a different name and be imported as well as the previous version and handled at that point within the program doing the importing)

I guess at this point I'm just curious about the basic commands for importing, saving, and modifying of files from within a python program.

All insight is appreciated,

Thanks,

PS: if you are wondering why I want to do all of this, it should be obvious, if not it would take a while to explain.

解决方案

Use inspect.getsource:

def a():
    pass

import inspect
print inspect.getsource(a)

Of course, this only works if you have access to the original source.

If you don't have the original source, the most you could get is the byte code (see dis.dis).

EDIT: The Python interactive shell is one example where inspect does not have access to the source.

这篇关于Python:字面上“打印”一个功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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