この世で最も役に立たない関数 by Python
"404 Blog Not Found:js/scheme/perl/ruby/C - この世で最も役に立たない関数
Pythonで書いてみた。多分py3kじゃ動かない。
def pointless(): global pointless; print 'Turning off myself', pointless; del pointless.func_globals['pointless']
py3kだとこう。
def pointless(): global pointless; print('Turning off myself', pointless) del pointless.__globals__['pointless']
動かすとこんな感じ。
>>> def pointless():
... global pointless;
... print 'Turning off myself', pointless;
... del pointless.func_globals['pointless']
...
>>> pointless()
Turning off myself
>>> pointless()
Traceback (most recent call last):
File "", line 1, in
NameError: name 'pointless' is not defined
あっちのコメント欄にこんなのがあるけど
def pointless(): global pointless; print 'Turning off myself', pointless; pointless=None
>>> def pointless(): global pointless; print 'Turning off myself', pointless; pointless=None
...
>>> pointless()
Turning off myself
>>> pointless()
Traceback (most recent call last):
File "", line 1, in
TypeError: 'NoneType' object is not callable
微妙に意味が違う。
もっとシンプルな方法(多分d:id:methaneさん)
def pointless(): global pointless; print 'Turning off myself', pointless; del pointless
な〜んだ、del pointlessで消えるんだ。……ん?
>>> pointless == pointless.__globals__['pointless']
True
なるほど。マイリマシタ。