0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

pythonでカッコのないメソッド呼び出しは何なのか。「メソッドオブジェクト」らしい。

Posted at

カッコのある通常のメソッド呼び出し

arr = [3,2,1]
arr.sort()
arr # [1, 2, 3]

カッコを付けないとメソッドオブジェクトが得られる

arr = [3,2,1]
arr.sort # <built-in method sort of list object at 0x10bdb3e40>
arr # [3, 2, 1]

メソッドは実行されていない。
このオブジェクトはなんやねん。

メソッドオブジェクトを一時変数に覚えさせておいて、あとで実行する

ってことが出来るようだ。

arr = [3,2,1]

do_sort = arr.sort
do_sort()

arr # [1, 2, 3]

参考

image.png

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

0
0
0

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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?