The “Hello World” from Hell, with exponential backslashes!

April 21, 2009 at 7:35 pm 1 comment

print eval("\"He\"+eval(\"\\\"ll\\\"+eval(\\\"\\\\\\\"o \\\\\\\"+eval(\\\\\\\"\\\\\\\\\\\\\\\"Wo\\\\\\\\\\\\\\\"\\\\\\\")+\\\\\\\"r\\\\\\\"\\\")+\\\"l\\\"\")+\"d\"")

All those backslashes are escaping each other, in order to escape the “.

To explain: in Python, the eval() statement evaluates a string as source code. eval(“5”) returns 5. Of course, this leads to messiness when you nest eval()s. For example: eval(“[5, eval(\”4\”)]”) returns [5, 4].
Further reading: “Escape Artist” by our good friend xkcd.

But look at that code at the top of the post. Couldn’t we automate the process of typing up all those backslashes?
Sure!


http://pastie.org/454063

Note the two print statements at the bottom. With the first uncommented, as it is now, the program prints:

http://pastie.org/454512 (note the tiny horizontal scrollbar)

With the first line commented out, and the second commented, you get the above mess interpreted as source code:
Hello World

Oh, the difference that an eval() makes.

Advertisements

Entry filed under: Uncategorized. Tags: , , , , , .

Doodlemaker: colorful random walk in Pygame Fublag has a Twitter

1 Comment Add your own

  • 1. fublag  |  April 21, 2009 at 8:08 pm

    From #xkcd:
    <Randall> At least
    <Randall> it wasn’t lisp nesting
    Don’t tempt me, I’ve been meaning to learn Scheme!

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Human Souls

wordpress visitor counter souls and counting...

%d bloggers like this: