Python 中 Hello World 的 10 种写法

2018-03-23 15:07:36 +08:00
 risent
干了一件比较蛋疼的事情,收集了一下 Python 中 Hello World 的 10 种写法。


https://gist.github.com/risent/7d8744bc528fbc967ed70f7658812d1e
5673 次点击
所在节点    Python
22 条回复
daiyuok
2018-03-23 15:44:44 +08:00
再推荐你一个--JPype
shoaly
2018-03-23 17:44:14 +08:00
我不小心联想到了 茴香豆的 4 种写法
yongzhong
2018-03-23 17:49:26 +08:00
加上一种,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
2018-03-23 17:51:24 +08:00
对不起,我根本不懂 py
GoLand
2018-03-23 19:48:29 +08:00
import __hello__

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

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/440788

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX