这在Python中可行吗? [英] Is this possible in Python?
问题描述
嗨
我想知道Python是否能够满足以下条件:定义一个函数
返回它的参数。
我的意思是:
def magic_function(arg):
......一些魔法代码...
那个表现如下:
断言magic_function(3 + 4)==" 3 + 4"
断言magic_function([i for i in range( 10)])==" i for i in range(10)]"
这根本不是微不足道的,可能需要一些字节码黑客攻击我
我无法做到自己但你是专家;-)
Alain
al********@yahoo.fr 写道:断言magic_function(3 + 4)==" 3 + 4"
断言magic_function([i for i in range(10)])==i for i in range(10)]" ;
它根本不是微不足道的,可能需要一些字节码黑客,我无法做你自己,但你是专家; - )
Guhhh ...你想要使用追溯系统并重新回到
源代码获取并解析调用魔法
函数的语句,有点像调试器。我不认为搞乱
字节码会有所帮助。
Paul Rubin写道:al ******** @ yahoo.fr 写道:断言magic_function( 3 + 4)==" 3 + 4"
断言magic_function([i for i in range(10)])==i for i in range(10)]"
它根本不是微不足道的,可能需要一些字节码黑客,我无法自己做,但你是专家;-)
Guhhh ...你想要使用追溯系统并回到源代码中来获取和解析调用魔术函数的语句,有点像调试器那样。
那是'单程
我不觉得搞乱
字节码会有所帮助。
嗯,以下包
http://packages.debian.org/stable/python/decompyle
可能允许这样的东西。
al ******** @ yahoo.fr 写道:嗨
我想知道Python是否具备以下功能:定义一个
返回其参数的函数。
我的意思是:
def magic_function(arg):
......一些魔法代码...
表现如下:
断言magic_function( 3 + 4)==" 3 + 4"
断言magic_function([i for i in range(10)])==i for i in range(10)]"
它根本不是微不足道的,可能需要一些字节码黑客,我无法自己做但你是专家;-)
Alain>
在评估之前存储参数并不适用于
Python。您必须破解编译器才能访问分析树。
您可以查看标准库
的编译器包,它允许访问AST。因此,您可以定义延迟评估和
即时编译某些特定的AST。我不建议这样做是为了真正的目的,但这是一个很好的练习,更快或者
以后你自己成为专家:)
Kay
Hi
I wonder if Python is capable of the following: define a function which
returns its argument.
I mean:
def magic_function(arg):
...... some magic code ...
that behaves the following way:
assert magic_function(3+4)=="3+4"
assert magic_function([i for i in range(10)])=="i for i in range(10)]"
It is not trivial at all and might require some bytecode hacking that i
am unable to do myself BUT you are the experts ;-)
Alain
al********@yahoo.fr writes:assert magic_function(3+4)=="3+4"
assert magic_function([i for i in range(10)])=="i for i in range(10)]"
It is not trivial at all and might require some bytecode hacking that i
am unable to do myself BUT you are the experts ;-)
Guhhh... you''d want to use the traceback system and reach back into
the source code to get and parse the statement that called the magic
function, sort of like a debugger does. I don''t think messing with
the bytecode would help.
Paul Rubin wrote:al********@yahoo.fr writes:assert magic_function(3+4)=="3+4"
assert magic_function([i for i in range(10)])=="i for i in range(10)]"
It is not trivial at all and might require some bytecode hacking that i
am unable to do myself BUT you are the experts ;-)
Guhhh... you''d want to use the traceback system and reach back into
the source code to get and parse the statement that called the magic
function, sort of like a debugger does.
That''s one way
I don''t think messing with
the bytecode would help.
Well, the following package
http://packages.debian.org/stable/python/decompyle
might permit something like that.
al********@yahoo.fr wrote:Hi
I wonder if Python is capable of the following: define a function which
returns its argument.
I mean:
def magic_function(arg):
...... some magic code ...
that behaves the following way:
assert magic_function(3+4)=="3+4"
assert magic_function([i for i in range(10)])=="i for i in range(10)]"
It is not trivial at all and might require some bytecode hacking that i
am unable to do myself BUT you are the experts ;-)
Alain
Storing arguments away before they are evaluated doesn''t work in
Python. You have to hack the compiler in order to access the parsetree.
You might take a look at the compiler package of the standard library
that enables access to ASTs. Thus you could define lazy evaluation and
just-in-time compilation of some particular ASTs. I do not recommend
doing this for real purposes, but it is a good excercise and sooner or
later you become an expert yourself :)
Kay
这篇关于这在Python中可行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!