这段代码如何在没有任何打印语句的情况下打印Hello World(How can this code print Hello World without any print statement)

编程入门 行业动态 更新时间:2024-10-26 16:22:04
这段代码如何在没有任何打印语句的情况下打印Hello World(How can this code print Hello World without any print statement)

我在Python中发现了这个代码,它在不使用字符串“Hello World”的情况下打印“Hello World”。 它是一行代码,一个表达式(即没有打印语句)。

(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 _, __, ___, ____, _____, ______, _______, ________: _)))

因为它是单行代码,所以这是一个格式良好的代码,它更具可读性。

它仅由函数,属性访问,列表,元组,基本数学,一个True和一个星标组成。 它具有最少的内建使用量( __import__ , getattr和chr每次一次)。

我很难理解它。 它有什么可能的解释吗?

顺便说一下,代码的作者解释了它的工作原理。

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.

最满意答案

这个问题的答案如下:代码避免了os.write()到stdout的文件描述符的print语句,该文件描述符为1 :

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

其余的解释详见https://benkurtovic.com/2014/06/01/obfuscating-hello-world.html 。 不要在这里总结,只要阅读原文!

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!

更多推荐

本文发布于:2023-08-07 21:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465931.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:没有任何   这段   语句   情况下   代码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!