Python:如何将参数传递给函数的__code__? [英] Python: How to pass arguments to the __code__ of a function?

查看:110
本文介绍了Python:如何将参数传递给函数的__code__?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下作品:

  def spam():
打印spam
exec垃圾邮件.__ code__)




垃圾邮件

但是如果 spam 需要参数?

<$ p $
printspam and,eggs
exec(spam .__ code__)
$ b


TypeError:spam()只需要1个参数(0给出)

假设我没有访问函数本身,但只访问一个代码对象,执行它时如何将参数传递给代码对象? eval是否可能?



编辑:由于大多数读者往往不相信这种用法,请参阅以下用例:



我想将小型Python函数保存到文件中,以便可以调用它们从另一台电脑。 (不用说在这里,这个用例严格限制了可能的功能。)腌制函数对象本身不能工作,因为它只保存定义函数的名称和模块。相反,我可以腌制函数的 __ code __ 。当我再次拔掉它时,当然对该功能的引用消失了,这就是为什么我不能调用该函数。我只是在运行时没有它。



另一个用例:



计算一些数据并将其存储在硬盘上的文件。计算会花费很多时间,所以我不想每次都执行这些函数,但是只有当函数的实现发生变化时才会执行。



我有一个版本这为整个模块而不是一个函数运行。它通过查看实现模块的文件的修改时间来工作。但是,如果我有许多功能,我不想在单个文件中分开,这不是一个选项。

解决方案

您可以将函数更改为而不是取任何参数?然后,从可以提供给 exec 的本地/全局变量查找这些变量:

 >>> def spam():
... printspam and,eggs
...
>>> exec(spam .__ code__,{'eggs':'pasta'})
垃圾邮件和面食

(为什么不把整个函数作为一个字符串发送?Pickle def spam(eggs):print'spam and',eggs exec 另一边的字符串(验证后)。


The following works:

def spam():
    print "spam"
exec(spam.__code__)

spam

But what if spam takes arguments?

def spam(eggs):
    print "spam and", eggs
exec(spam.__code__)

TypeError: spam() takes exactly 1 argument (0 given)

Given, that I don't have access to the function itself but only to a code object, how can I pass arguments to the code object when executing it? Is it possible with eval?

Edit: Since most readers tend not to believe in the usefulness of this, see the following use case:

I want to save small Python functions to a file so that they can be called e.g. from another computer. (Needless to say here that this usecase restricts the possible functions severely.) Pickling the function object itself can't work because this only saves the name and the module where the function is defined. Instead, I could pickle the __code__ of the function. When I unpickle it again, of course the reference to the function vanished, which is why I can't call the function. I simply don't have it at runtime.

Another usecase:

I work on several functions in one file that calculate some data and store it on the hard drive. The calculations consume a lot of time so I don't want to execute the functions every time, but only when the implementation of the function changed.

I have a version of this running for a whole module instead of a function. It works by looking at the modification time of the file where the module is implemented in. But this is not an option if I have many functions that I don't want to separate in single files.

解决方案

Can you change the function to not take any arguments? The variables is then looked up from the locals/globals where you can supply into exec:

>>> def spam():
...   print "spam and", eggs
... 
>>> exec(spam.__code__, {'eggs':'pasta'})
spam and pasta

(Why not just send the whole function as a string? Pickle "def spam(eggs): print 'spam and', eggs", and exec the string (after verification) on the other side.)

这篇关于Python:如何将参数传递给函数的__code__?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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