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?

Subscriptableという概念

0
Posted at

きっかけ

Numpy配列を作成したところ、エラー文にて「Subscriptable」という単語に出会った

問題のコードとエラー文

left = np.array[0,10000,20000,30000,4000,50000,60000]
TypeError: 'builtin_function_or_method' object is not subscriptable

エラー文の意味

組み込み関数やメソッドのオブジェクトはSubscriptableではない

Subscriptableの意味

英単語の意味:「subscriptable = 添字でアクセスできる」
Pythonにおける意味:「番号を指定して中身を取り出せるもの」

Subscriptableなオブジェクトとは何か

例.リストはSUbscriptableである。

fruits = [orange,apple, grape]
fruits[1] #番号を指定する

結果

"apple"

コードの何が問題か

np.arrayは関数なので、要素とか順番という概念がない。
エラーの示す内容は、「関数に対して[]を使っているけどそれはできない」ということ。

Subscriptableなobjectを満たす条件

obj.__getitem__(index)

上記のメソッドを持っていると、Subscriptableであると言える。
つまり、__getitem__関数を使い、obj[]を使えるものがSubscrptableである。

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?