Is this possible in Python?

Terry Reedy tjreedy at udel.edu
Mon Mar 13 13:13:56 EST 2006


<alainpoint at yahoo.fr> wrote in message 
news:1142238871.199749.243470 at p10g2000cwp.googlegroups.com...
> 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)]"

The arguments to Python functions are Python objects.  In order to return 
the argument as a string, you must pass it as a string.

>>> def magic(s): return s, eval(s)

>>> magic('3+4')
('3+4', 7)
>>> magic('[i**2 for i in [1,2,3]]')
('[i**2 for i in [1,2,3]]', [1, 4, 9])

Terry Jan Reedy






More information about the Python-list mailing list