推荐学习书目
› 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 · Mar 23, 2018 · 7464 views
    This topic created in 3163 days ago, the information mentioned may be changed or developed.
    干了一件比较蛋疼的事情,收集了一下 Python 中 Hello World 的 10 种写法。


    Supplement 1  ·  Mar 24, 2018

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

    22 replies  •  2018-03-27 15:04:00 +08:00
    daiyuok
    daiyuok
    Mar 23, 2018
    1
    再推荐你一个--JPype
    shoaly
    shoaly
    Mar 23, 2018❤️1
    2
    我不小心联想到了 茴香豆的 4 种写法
    yongzhong
    yongzhong
    Mar 23, 2018❤️1
    3
    加上一种,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
    pkookp8
    Mar 23, 2018 via Android
    4
    对不起,我根本不懂 py
    GoLand
    GoLand
    Mar 23, 2018❤️4
    5
    import __hello__

    这个很少人知道吧?
    assad
    assad
    Mar 23, 2018 via Android❤️1
    6
    Python 不是将就只用一种方法做事么?
    lingo
    lingo
    Mar 23, 2018
    7
    有点强行了。。。
    dd99iii
    dd99iii
    Mar 23, 2018
    8
    疼
    860670496
    860670496
    Mar 23, 2018
    9
    @yongzhong #3 我觉得我要专门撸个轮子帮我在 IDE 里数下划线的字符个数
    hellolleh
    hellolleh
    Mar 23, 2018
    10
    5. ctypes
    这个好像不对吧,python 里面字符串是一个 list,你这样传给 c 的函数,c 只能接受一个 h,后面的是没法传递过去的。
    risent
    risent
    OP
    Mar 23, 2018
    11
    @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
    hellolleh
    Mar 23, 2018
    12
    @risent 我是 python3,确实是这样。python2 估计是自动转换的了。
    risent
    risent
    OP
    Mar 23, 2018
    13
    @assad 主要是针对不同的情况, 比如需要用到一个 C/C++ 的库,那么就可以很快的通过 ctypes 或者 cffi 的方式快速的撸一个 binding 出来,
    或者如果想对某一个模块 /功能在性能上优化一下,那么可以将其直接用 Cython 或者 Rust 直接重写,然后提供给 Python 来调用。
    hellolleh
    hellolleh
    Mar 23, 2018
    14
    alexyangjie
    alexyangjie
    Mar 23, 2018 via iPhone
    15
    也想到了茴香豆。。。。
    SingeeKing
    SingeeKing
    Mar 23, 2018
    16
    @yongzhong #3 有 3 的版本吗
    risent
    risent
    OP
    Mar 23, 2018
    17
    @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
    neocanable
    Mar 23, 2018
    18
    @pkookp8 py 不是跑友的意思么?
    pwrliang
    pwrliang
    Mar 24, 2018 via Android
    19
    我觉得挺好的,收藏了,尤其是 python 调用 c 的代码
    hotea
    hotea
    Mar 26, 2018
    20
    @GoLand 学到了
    JerseyCafe
    JerseyCafe
    Mar 27, 2018
    21
    666666
    est
    est
    Mar 27, 2018
    22
    最装逼的一种写法你没列出

    import __hello__
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   3009 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 60ms · UTC 00:51 · PVG 08:51 · LAX 16:51 · JFK 19:51
    ♥ Do have faith in what you're doing.