Python 中 Hello World 的 10 种写法 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
risent
V2EX    Python

Python 中 Hello World 的 10 种写法

  •  
  •   risent 2018-03-23 15:07:36 +08:00 6342 次点击
    这是一个创建于 2766 天前的主题,其中的信息可能已经有所发展或是发生改变。
    干了一件比较蛋疼的事情,收集了一下 Python 中 Hello World 的 10 种写法。


    第 1 条附言    2018-03-24 14:39:03 +08:00

    附:发现目前用 Rust 来扩展/优化 Python 是一种逐渐很流行的做法, hg 已经在用Rust 替代其 C 扩展来优化速度,放几条相关信息:

    22 条回复    2018-03-27 15:04:00 +08:00
    daiyuok
        1
    daiyuok  
       2018-03-23 15:44:44 +08:00
    再推荐你一个--JPype
    shoaly
        2
    shoaly  
       2018-03-23 17:44:14 +08:00   1
    我不小心联想到了 茴香豆的 4 种写法
    yongzhong
        3
    yongzhong  
       2018-03-23 17:49:26 +08:00   1
    加上一种,py2

    (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 _, __, ___, ____, _____, ______, _______, ________: _
    )
    )
    )
    pkookp8
        4
    pkookp8  
       2018-03-23 17:51:24 +08:00 via Android
    对不起,我根本不懂 py
    GoLand
        5
    GoLand  
       2018-03-23 19:48:29 +08:00   4
    import __hello__

    这个很少人知道吧?
    assad
        6
    assad  
       2018-03-23 19:58:20 +08:00 via Android   1
    Python 不是将就只用一种方法做事么?
    lingo
        7
    lingo  
       2018-03-23 19:59:51 +08:00
    有点强行了。。。
    dd99iii
        8
    dd99iii  
       2018-03-23 20:45:27 +08:00
    860670496
        9
    860670496  
       2018-03-23 20:50:35 +08:00
    @yongzhong #3 我觉得我要专门撸个轮子帮我在 IDE 里数下划线的字符个数
    hellolleh
        10
    hellolleh  
       2018-03-23 21:21:52 +08:00
    5. ctypes
    这个好像不对吧,python 里面字符串是一个 list,你这样传给 c 的函数,c 只能接受一个 h,后面的是没法传递过去的。
    risent
        11
    risent  
    OP
       2018-03-23 21:41:44 +08:00
    @hellolleh 这个是会自动转换的

    > None, integers, longs, byte strings and unicode strings are the only native Python objects that can directly be used as parameters in these function calls. None is passed as a C NULL pointer, byte strings and unicode strings are passed as pointer to the memory block that contains their data (char * or wchar_t *). Python integers and Python longs are passed as the platforms default C int type, their value is masked to fit into the C type.


    https://docs.python.org/2/library/ctypes.html
    hellolleh
        12
    hellolleh  
       2018-03-23 21:49:49 +08:00
    @risent 我是 python3,确实是这样。python2 估计是自动转换的了。
    risent
        13
    risent  
    OP
       2018-03-23 21:50:02 +08:00
    @assad 主要是针对不同的情况, 比如需要用到一个 C/C++ 的库,那么就可以很快的通过 ctypes 或者 cffi 的方式快速的撸一个 binding 出来,
    或者如果想对某一个模块 /功能在性能上优化一下,那么可以将其直接用 Cython 或者 Rust 直接重写,然后提供给 Python 来调用。
    hellolleh
        14
    hellolleh  
       2018-03-23 21:55:36 +08:00
    alexyangjie
        15
    alexyangjie  
       2018-03-23 22:00:41 +08:00 via iPhone
    也想到了茴香豆。。。。
    SingeeKing
        16
    SingeeKing  
    PRO
       2018-03-23 22:32:51 +08:00
    @yongzhong #3 有 3 的版本吗
    risent
        17
    risent  
    OP
       2018-03-23 23:20:53 +08:00
    @hellolleh 确实,示例代码没考虑 Python3, Python3 中应该是下面这样子

    ```
    import ctypes

    hello_lib = ctypes.cdll.LoadLibrary('hello.so')

    hello = hello_lib.hello
    hello(b'Hello World')
    hello('Hello World'.encode('ascii'))

    ```
    neocanable
        18
    neocanable  
       2018-03-23 23:52:26 +08:00
    @pkookp8 py 不是跑友的意思么?
    pwrliang
        19
    pwrliang  
       2018-03-24 09:11:46 +08:00 via Android
    我觉得挺好的,收藏了,尤其是 python 调用 c 的代码
    hotea
        20
    hotea  
       2018-03-26 18:21:26 +08:00
    @GoLand 学到了
    JerseyCafe
        21
    JerseyCafe  
       2018-03-27 09:45:57 +08:00
    666666
    est
        22
    est  
       2018-03-27 15:04:00 +08:00
    最装逼的一种写法你没列出

    import __hello__
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2634 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 27ms UTC 14:42 PVG 22:42 LAX 07:42 JFK 10:42
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86