[Tutor] Converting code to string

bhaaluu bhaaluu at gmail.com
Thu Aug 23 22:54:44 CEST 2007


Greetings,

The following is from:
http://www.hetland.org/coding/
##################
Self-Printing One-Liner

If you run this one-liner at a command prompt, it should print out a
copy of itself (write it as one continuous line, without the line
break):

python -c "x='python -c %sx=%s; print x%%(chr(34),repr(x),chr(34))%s';
print x%(chr(34),repr(x),chr(34))"

Not very useful, but kinda fun… I just saw some other self-printing
programs and thought it would be interesting to make a one-liner
version.

##################

Hopefully helpful!
--
bhaaluu at gmail dot com

On 8/23/07, Bernard Lebel <3dbernard at gmail.com> wrote:
> Hello,
>
> I'm looking for a way to convert Python code into a string.
>
> For example, let say I have this function:
>
> def myFunc():
>     print 'hello world'
>
>
> Now in the same module, I'd like to take this function and convert it
> into a string:
>
> """def myFunc():
>     print 'hello world'\n"""
>
>
>
> Thanks
> Bernard
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list