Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can this code print Hello World without any print statement

Tags:

python

I found this code in Python, which prints "Hello World" without the use of the string "Hello World". It's a one line code, a single expression (i.e. no print statement).

(lambda _, __, ___, ____, _____, ______, _______, ________: getattr(__import__(True.__class__.__name__[_] + [].__class__.__name__[__]), ().__class__.__eq__.__class__.__name__[:__] + ().__iter__().__class__.__name__[_____:________])(_, (lambda _, __, ___: _(_, __, ___))(lambda _, __, ___: chr(___ % __) + _(_, __, ___ // __) if ___ else (lambda: _).func_code.co_lnotab, _ << ________, (((_____ << ____) + _) << ((___ << _____) - ___)) + (((((___ << __) - _) << ___) + _) << ((_____ << ____) + (_ << _))) + (((_______ << __) - _) << (((((_ << ___) + _)) << ___) + (_ << _))) + (((_______ << ___) + _) << ((_ << ______) + _)) + (((_______ << ____) - _) << ((_______ << ___))) + (((_ << ____) - _) << ((((___ << __) + _) << __) - _)) - (_______ << ((((___ << __) - _) << __) + _)) + (_______ << (((((_ << ___) + _)) << __))) - ((((((_ << ___) + _)) << __) + _) << ((((___ << __) + _) << _))) + (((_______ << __) - _) << (((((_ << ___) + _)) << _))) + (((___ << ___) + _) << ((_____ << _))) + (_____ << ______) + (_ << ___))))(*(lambda _, __, ___: _(_, __, ___))((lambda _, __, ___: [__(___[(lambda: _).func_code.co_nlocals])] + _(_, __, ___[(lambda _: _).func_code.co_nlocals:]) if ___ else []), lambda _: _.func_code.co_argcount, (lambda _: _, lambda _, __: _, lambda _, __, ___: _, lambda _, __, ___, ____: _, lambda _, __, ___, ____, _____: _, lambda _, __, ___, ____, _____, ______: _, lambda _, __, ___, ____, _____, ______, _______: _, lambda _, __, ___, ____, _____, ______, _______, ________: _)))

As it is a single line code, Here's a well formatted code which is more readable.

It is made up of only functions, attribute access, lists, tuples, basic math, one True, and one star-args. It has minimal builtin usage (__import__, getattr, and chr once each).

It's really hard for me to understand it. Is there any possible explanation of what it does?

Here, by the way, is where the author of the code explains how it works.

like image 989
Himanshu Mishra Avatar asked Jul 14 '15 07:07

Himanshu Mishra


People also ask

How can I print Hello World Without cout?

Show activity on this post. const char * str = "Hello World\n"; fprintf(stdout, str); fputs(str, stdout); for (int i=0; str[i]!= 0; ++i) putchar(str[i]); for (int i=0; str[i]!= 0; ++i) putc(str[i], stdout); for (int i=0; str[i]!=

How can I print my name without printf?

How do you print your name in C language without using any printing function like printf and all? int main() { write(1, "Hello World", strlen("Hello World")); return 0; } This could be one way of printing without using printf, sprintf, and all related functions.


1 Answers

The answer to the question as written: The code avoids a print statement by os.write()ing to stdout's file descriptor, which is 1:

getattr(__import__("os"), "write")(1, "Hello world!\n")

The rest of the explanation is detailed at https://benkurtovic.com/2014/06/01/obfuscating-hello-world.html. Instead of a summary here, just read the original!

like image 170
Chris Culter Avatar answered Nov 11 '22 21:11

Chris Culter