Is this possible in Python?
Caleb Hattingh
a at b.c
Mon Mar 13 13:35:53 EST 2006
Hi
I don't think this is what you want (a string representation of the
argument passed to a function as that argument is at runtime is way
beyond my abilities), but this can retrieve the literal text in the
function call as it appears in the .py file, assuming you have the .py
file available and not just the .pyc:
### Call this file "pyfunargtest.py"
def fun(i):
pass
fun(8+7)
def test():
fname = 'pyfunargtest.py'
testfunname = 'f'
flines = open(fname,'r').readlines()
for i in flines:
if i.find(testfunname)>-1 and i.find('def '+testfunname)==-1:
s = i
break
leftbracketposition = s.find('(')
rightbracketposition = s.find(')')
arg = s[leftbracketposition+1:rightbracketposition]
return arg
print test()
### Output:
# 8+7
alainpoint at 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
>
More information about the Python-list
mailing list