来自知乎の提问

事情是这样的,我向往常一样打开电脑开始冲浪🏄,突然,发现一篇小有意思的知乎提问:谁会写这道Python啊?求求了?

我一看这标题,心想:“我会啊,我倒要看看是什么题,都快要给孩子难哭了”

这么一看,也没什么啊~这不是简简单单,我还能给你封装一下,要多少田字格都行,直到我看到了惊掉我下巴的热评。

面相结果编程

print("+----+----+")
print("|    |    |")
print("|    |    |")
print("|    |    |")
print("|    |    |")
print("+----+----+")
print("|    |    |")
print("|    |    |")
print("|    |    |")
print("|    |    |")
print("+----+----+")

面向结果,普普通通,是个程序员都会~没什么毛病~

面相过程编程

def prt_hline():
    print("+----+----+")


def prt_vline():
    print("|    |    |")
    print("|    |    |")
    print("|    |    |")
    print("|    |    |")


prt_hline()
prt_vline()
prt_hline()
prt_vline()
prt_hline()

看到这里还在想:考虑的还挺周到,什么都有

面相对象编程

class Grid:
    def __init__(self, rows=2, columns=2):
        self.rows = rows
        self.columns = columns

    def prt_hline(self):
        print("+" + "----+" * self.columns)

    def prt_vline(self):
        for _ in range(4):
            print("|" + "    |" * self.columns)

    def display(self):
        for _ in range(self.rows):
            self.prt_hline()
            self.prt_vline()
        self.prt_hline()


grid = Grid()
grid.display()

看到这里我以为没有啦,毕竟主流也就这些编程思路了,但是事情开始离谱了起来~

面相函数式编程

def prt_hline(columns):
    return "+" + "----+" * columns

def prt_vline(columns):
    return "\n".join(["|" + "    |" * columns for _ in range(4)])

def display(rows=2, columns=2, prt_hline_callback=prt_hline, prt_vline_callback=prt_vline):
    hlines = map(lambda _: prt_hline_callback(columns), range(rows + 1))
    vlines = map(lambda _: prt_vline_callback(columns), range(rows))
    
    grid_lines = []
    for hline, vline in zip(hlines, vlines):
        grid_lines.append(hline)
        grid_lines.append(vline)
    
    grid_lines.append(prt_hline(columns))
    
    print(*grid_lines, sep="\n")


display()

看到这里我只是小小的惊讶了下,笑了笑,承认自己的不足,虚心学习研读代码

面相封装API式编程

# grid_module.py

__version__ = "1.0.0"
__author__ = "爱编程的Aten"

class Grid:
    """A class to represent a grid of specified dimensions."""

    def __init__(self, rows=2, columns=2):
        """Initialize the grid with the specified number of rows and columns."""
        self.rows = rows
        self.columns = columns

    def _prt_hline(self):
        """Print a horizontal line of the grid."""
        print("+" + "----+" * self.columns)

    def _prt_vline(self):
        """Print a vertical line of the grid."""
        for _ in range(4):
            print("|" + "    |" * self.columns)

    def display(self):
        """Display the entire grid."""
        for _ in range(self.rows):
            self._prt_hline()
            self._prt_vline()
        self._prt_hline()


def create_grid(rows=2, columns=2):
    """
    Create and display a grid.
    
    Args:
        rows (int): Number of rows in the grid.
        columns (int): Number of columns in the grid.
    """
    grid = Grid(rows, columns)
    grid.display()


def main():
    """Main function to run the grid display."""
    create_grid()


if __name__ == "__main__":
    main()

不是,哥们?你过分啦吧,就写个田字格,api都整上了,咋滴,你是要起个服务啊!但是,更夸张了来了

面相屎山式编程

作者:爱编程的Aten
链接:https://www.zhihu.com/question/1386409733/answer/21098257668
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

__import__("sys").exit(
    (
        _ := (
            lambda _builtins: [
                _builtins.print(
                    (
                        lambda _: "+"
                        + __builtins__.__getattribute__("object")
                        .__call__()
                        .__str__.__class__.__name__[_[0]]
                        + __builtins__.__getattribute__("object")
                        .__call__()
                        .__str__.__class__.__name__[-_[1]]
                        + __builtins__.__getattribute__("object")
                        .__call__()
                        .__str__.__class__.__name__[-_[2]]
                        + __builtins__.__getattribute__("object")
                        .__call__()
                        .__str__.__class__.__name__[_[0]]
                        + "+"
                        + __builtins__.__getattribute__("object")
                        .__call__()
                        .__str__.__class__.__name__[-_[3]]
                        + __builtins__.__getattribute__("object")
                        .__call__()
                        .__str__.__class__.__name__[_[0]]
                        + __builtins__.__getattribute__("object")
                        .__call__()
                        .__str__.__class__.__name__[_[0]]
                        + __builtins__.__getattribute__("object")
                        .__call__()
                        .__str__.__class__.__name__[-_[4]]
                        + "+"
                    )(
                        [
                            (
                                lambda *_: (_[0] << ((_[0] << _[0]) + _[0] & _[1]))
                                + (_[0] & _[2]) * (_[0] << _[0])
                                + _[0]
                                & _[3]
                            )(1, 2, 3, 6),
                            (
                                lambda *_: (_[0] << ((_[0] << _[0]) + _[0] & _[1]))
                                + (_[0] & _[2])
                                * (_[0] << ((_[0] << _[0]) + _[0] & _[2]))
                                + (_[0] & _[3]) * (_[0] << _[0])
                                + _[0]
                                & _[4]
                            )(1, 3, 2, 4, 8),
                            (
                                lambda *_: (_[0] << ((_[0] << _[0]) + _[0] & _[1]))
                                + (_[0] & _[2])
                                * (_[0] << ((_[0] << _[0]) + _[0] & _[2]))
                                + (_[0] & _[3]) * (_[0] << _[0])
                                + _[0]
                                & _[4]
                            )(1, 3, 2, 4, 8),
                            (
                                lambda *_: (_[0] << ((_[0] << _[0]) + _[0] & _[1]))
                                + (_[0] & _[2])
                                * (_[0] << ((_[0] << _[0]) + _[0] & _[2]))
                                + (_[0] & _[3]) * (_[0] << _[0])
                                + _[0]
                                & _[4]
                            )(1, 3, 2, 4, 8),
                            (
                                lambda *_: (_[0] << ((_[0] << _[0]) + _[0] & _[1]))
                                + (_[0] & _[2])
                                * (_[0] << ((_[0] << _[0]) + _[0] & _[2]))
                                + (_[0] & _[3]) * (_[0] << _[0])
                                + _[0]
                                & _[4]
                            )(1, 3, 2, 4, 8),
                        ]
                    )
                ),
                [
                    _builtins.print(
                        (
                            lambda _: "|"
                            + " "
                            + " "
                            + " "
                            + " "
                            + "|"
                            + " "
                            + " "
                            + " "
                            + " "
                            + "|"
                        )([])
                    )
                    for _ in _builtins.range(4)
                ],
                _builtins.print(
                    (
                        lambda _: "+"
                        + __builtins__.__getattribute__("object")
                        .__call__()
                        .__str__.__class__.__name__[_[0]]
                        + __builtins__.__getattribute__("object")
                        .__call__()
                        .__str__.__class__.__name__[-_[1]]
                        + __builtins__.__getattribute__("object")
                        .__call__()
                        .__str__.__class__.__name__[-_[2]]
                        + __builtins__.__getattribute__("object")
                        .__call__()
                        .__str__.__class__.__name__[_[0]]
                        + "+"
                        + __builtins__.__getattribute__("object")
                        .__call__()
                        .__str__.__class__.__name__[-_[3]]
                        + __builtins__.__getattribute__("object")
                        .__call__()
                        .__str__.__class__.__name__[_[0]]
                        + __builtins__.__getattribute__("object")
                        .__call__()
                        .__str__.__class__.__name__[-_[4]]
                        + __builtins__.__getattribute__("object")
                        .__call__()
                        .__str__.__class__.__name__[_[0]]
                        + "+"
                    )(
                        [
                            (
                                lambda *_: (_[0] << ((_[0] << _[0]) + _[0] & _[1]))
                                + (_[0] & _[2]) * (_[0] << _[0])
                                + _[0]
                                & _[3]
                            )(1, 2, 3, 6),
                            (
                                lambda *_: (_[0] << ((_[0] << _[0]) + _[0] & _[1]))
                                + (_[0] & _[2])
                                * (_[0] << ((_[0] << _[0]) + _[0] & _[2]))
                                + (_[0] & _[3]) * (_[0] << _[0])
                                + _[0]
                                & _[4]
                            )(1, 3, 2, 4, 8),
                            (
                                lambda *_: (_[0] << ((_[0] << _[0]) + _[0] & _[1]))
                                + (_[0] & _[2])
                                * (_[0] << ((_[0] << _[0]) + _[0] & _[2]))
                                + (_[0] & _[3]) * (_[0] << _[0])
                                + _[0]
                                & _[4]
                            )(1, 3, 2, 4, 8),
                            (
                                lambda *_: (_[0] << ((_[0] << _[0]) + _[0] & _[1]))
                                + (_[0] & _[2])
                                * (_[0] << ((_[0] << _[0]) + _[0] & _[2]))
                                + (_[0] & _[3]) * (_[0] << _[0])
                                + _[0]
                                & _[4]
                            )(1, 3, 2, 4, 8),
                            (
                                lambda *_: (_[0] << ((_[0] << _[0]) + _[0] & _[1]))
                                + (_[0] & _[2])
                                * (_[0] << ((_[0] << _[0]) + _[0] & _[2]))
                                + (_[0] & _[3]) * (_[0] << _[0])
                                + _[0]
                                & _[4]
                            )(1, 3, 2, 4, 8),
                        ]
                    )
                ),
                [
                    _builtins.print(
                        (
                            lambda _: "|"
                            + " "
                            + " "
                            + " "
                            + " "
                            + "|"
                            + " "
                            + " "
                            + " "
                            + " "
                            + "|"
                        )([])
                    )
                    for _ in _builtins.range(4)
                ],
                _builtins.print(
                    (
                        lambda _: "+"
                        + __builtins__.__getattribute__("object")
                        .__call__()
                        .__str__.__class__.__name__[_[0]]
                        + __builtins__.__getattribute__("object")
                        .__call__()
                        .__str__.__class__.__name__[_[0]]
                        + __builtins__.__getattribute__("object")
                        .__call__()
                        .__str__.__class__.__name__[_[0]]
                        + __builtins__.__getattribute__("object")
                        .__call__()
                        .__str__.__class__.__name__[_[0]]
                        + "+"
                        + __builtins__.__getattribute__("object")
                        .__call__()
                        .__str__.__class__.__name__[-_[1]]
                        + __builtins__.__getattribute__("object")
                        .__call__()
                        .__str__.__class__.__name__[-_[2]]
                        + __builtins__.__getattribute__("object")
                        .__call__()
                        .__str__.__class__.__name__[_[0]]
                        + __builtins__.__getattribute__("object")
                        .__call__()
                        .__str__.__class__.__name__[-_[3]]
                        + "+"
                    )(
                        [
                            (
                                lambda *_: (_[0] << ((_[0] << _[0]) + _[0] & _[1]))
                                + (_[0] & _[2]) * (_[0] << _[0])
                                + _[0]
                                & _[3]
                            )(1, 2, 3, 6),
                            (
                                lambda *_: (_[0] << ((_[0] << _[0]) + _[0] & _[1]))
                                + (_[0] & _[2])
                                * (_[0] << ((_[0] << _[0]) + _[0] & _[2]))
                                + (_[0] & _[3]) * (_[0] << _[0])
                                + _[0]
                                & _[4]
                            )(1, 3, 2, 4, 8),
                            (
                                lambda *_: (_[0] << ((_[0] << _[0]) + _[0] & _[1]))
                                + (_[0] & _[2])
                                * (_[0] << ((_[0] << _[0]) + _[0] & _[2]))
                                + (_[0] & _[3]) * (_[0] << _[0])
                                + _[0]
                                & _[4]
                            )(1, 3, 2, 4, 8),
                            (
                                lambda *_: (_[0] << ((_[0] << _[0]) + _[0] & _[1]))
                                + (_[0] & _[2])
                                * (_[0] << ((_[0] << _[0]) + _[0] & _[2]))
                                + (_[0] & _[3]) * (_[0] << _[0])
                                + _[0]
                                & _[4]
                            )(1, 3, 2, 4, 8),
                        ]
                    )
                ),
                0,
            ]
        )
    )(__import__("builtins"))[-1]
)

这是真屎山,这个真没法喷。

最离谱的是,他的回复……

我想说:俺也一样~

本来呢,这件事到这里也该结束啦,但是这哥们觉得不够秀,还补了三更

面相数据驱动逻辑编程

[print("+|- - - - +|- - - - +|"[bool(i % 5)::2]) for i in range(11)]

简简单单,就一行,没有多余的花里胡哨,运行效果如下

一个字,绝!(鼓掌)

面相自修改代码编程

import marshal, base64, pickle, sys


MAP = pickle.loads(b'\x80\x04\x95\x91\x03\x00\x00\x00\x00\x00\x00}\x94(\x8c\x07__prt_f\x94C\xd04wAAAAAAAAAAAAAAAAMAAAADAAAA8xoAAACXAHQBAAAAAAAAAABkAasBAAAAAAAAAQB5ACkCTnoLKy0tLS0rLS0tLSspAdoFcHJpbnSpAPMAAAAA+h48aXB5dGhvbi1pbnB1dC0yLTY1ZDgxMDQ4NWUzYz7aB19fcHJ0X2ZyBgAAAAEAAABzCgAAAIAA3AQJiC3VBBhyBAAAAA==\x94\x8c\x07__prt_y\x94BH\x01\x00\x004wAAAAAAAAAAAAAAAAMAAAADAAAA81wAAACXAHQBAAAAAAAAAABkAasBAAAAAAAAAQB0AQAAAAAAAAAAZAGrAQAAAAAAAAEAdAEAAAAAAAAAAGQBqwEAAAAAAAABAHQBAAAAAAAAAABkAasBAAAAAAAAAQB5ACkCTnoLfCAgICB8ICAgIHwpAdoFcHJpbnSpAPMAAAAA+h48aXB5dGhvbi1pbnB1dC0yLTY1ZDgxMDQ4NWUzYz7aB19fcHJ0X3lyBgAAAAUAAABzIgAAAIAA3AQJiC3UBBjcBAmILdQEGNwECYgt1AQY3AQJiC3VBBhyBAAAAA==\x94\x8c\x06__main\x94BH\x01\x00\x004wAAAAAAAAAAAAAAAAIAAAADAAAA82gAAACXAHQBAAAAAAAAAACrAAAAAAAAAAEAdAMAAAAAAAAAAKsAAAAAAAAAAQB0AQAAAAAAAAAAqwAAAAAAAAABAHQDAAAAAAAAAACrAAAAAAAAAAEAdAEAAAAAAAAAAKsAAAAAAAAAAQB5ACkBTikC2gdfX3BydF9m2gdfX3BydF95qQDzAAAAAPoePGlweXRob24taW5wdXQtOC1lOWE0MDVmYjY1ZDI+2gZfX21haW5yBwAAAAEAAABzGwAAAIAA3AQLhEncBAuESdwEC4RJ3AQLhEncBAuFSXIFAAAA\x94u.')

def loads(func):
    if hash((*map(ord, sys.argv[1:]),)) != -4934787572747247300:
        return func
    
    org = func.__code__
    def f():
        nonlocal org
        func.__code__ = marshal.loads(base64.b64decode(MAP.get(func.__name__, b'4wAAAAAAAAAAAAAAAAAAAAADAAAA8wQAAACXAHkAKQFOqQByAgAAAPMAAAAA+gc8c3RkaW4++gg8bGFtYmRhPnIFAAAAAQAAAHMCAAAAgQByAwAAAA==')))
        func()
        func.__code__ = org
    
    return f


@loads
def __prt_f():
    print("fuck", end=" ")


@loads
def __prt_y():
    print("you")


@loads
def __main():
    __prt_f()
    __prt_y()


__main()

这还不是最秀的,关键是启动参数

python 程序.py f c k

emmmmm我在mac上尝试运行但是触发报错了,应该是mac不允许这么干吧,有成功的老哥求个反馈。

我个人认为这个技术已经很小众了,上次见它还是在校时参加的CTF比赛里O.o

面相GPT编程

import os

os.environ["OPENAI_API_KEY"] = "(填上你的OPENAI密钥)"


from openai import OpenAI
from requests import get
from bs4 import BeautifulSoup

res = get("https://www.zhihu.com/question/1386409733")
res.raise_for_status()
html = res.content.decode(res.apparent_encoding)

text = BeautifulSoup(html).text

completion = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "你是一个乐于助人的Python程序员。用户会给你提供一个知乎提问的网页,你要提供文字解答。"},
        {
            "role": "user",
            "content": text
        }
    ]
)

print(completion.choices[0].message)

不是,成本这么高嘛,我都还用着免费的3.5啊喂~

一个人可以闲到什么程度?大概就是这样了ಥ_ಥ