LoginSignup
0
1

More than 1 year has passed since last update.

Pythonのlambda式もオブジェクトであることに気がついた話

Posted at

経緯

先日私は友人にPythonのクソコードを送りつけようと思い、普段は組まないような構文を積極的に書いて動作するか試していました。
そしてこの一文を打ったのです。

print(lambda x: x+1)

結果:<function <lambda> at 0x0000027C83B613A0>

そっか、関数もオブジェクトなのね...

ん?

思いついてしまった

じゃあ...

(lambda x: x+1, lambda x: x+2, lambda x: x+3)[0](1)

結果:2

ちゃんと出来るやん...
もう少しいじってみよう

[i(3) for i in (lambda x: x+1, lambda x: x+2, lambda x: x+3)]

結果:[4, 5, 6]

oh...(クソコードの予感)

まとめ

結構面白いことが出来るんじゃないかなと思いました。

なお実用性()

みなさんもぜひ良い使い方を考えてみてください。(丸投げ)

参考文献

式 (expression)

0
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1