推荐学习书目
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 · 6822 views
    This topic created in 2979 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
        1
    daiyuok  
       Mar 23, 2018
    再推荐你一个--JPype
    shoaly
        2
    shoaly  
       Mar 23, 2018   ❤️ 1
    我不小心联想到了 茴香豆的 4 种写法
    yongzhong
        3
    yongzhong  
       Mar 23, 2018   ❤️ 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  
       Mar 23, 2018 via Android
    对不起,我根本不懂 py
    GoLand
        5
    GoLand  
       Mar 23, 2018   ❤️ 4
    import __hello__

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

    import __hello__
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5316 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 100ms · UTC 07:35 · PVG 15:35 · LAX 00:35 · JFK 03:35
    ♥ Do have faith in what you're doing.