共通操作や主な機能・特徴については覚えた方が良い。しかし、細かい個々の関数については覚える必要はない。個々の機能が必要になった時に「調べて使える」ようになろう。すなわち「調べ方」を学ぶ必要がある。
- 主な調べ方1: help()
- 簡易ドキュメントhelpを使って調べる例。
- help(str)
- help(str.find)
- 簡易ドキュメントhelpを使って調べる例。
- 主な調べ方2: 公式ドキュメント(リファレンス)
- 主な調べ方3:
- 該当オブジェクトについて、どんな関数が用意されているかを調べる
- インタプリタの場合
- 例えばstrオブジェクトについて調べたい場合。
- インタプリタ上でstrオブジェクトを保存した変数を用意する。ここでは
temp = 'hoge'と保存したとする。 - インタプリタ上で「変数名.」と入力した状態でTABキーで補完しようとすると、どのような関数が用意されてるかの一覧が出力される。前述の例に合わせて、
temp.まで入力してTABキーを押すと次のように出力される。一覧によると、例えば「capitalize(」という関数が用意されているらしい。この関数について調べたいならhelp(temp.capitalize)として、help()を利用しよう。
- インタプリタ上でstrオブジェクトを保存した変数を用意する。ここでは
- 例えばstrオブジェクトについて調べたい場合。
- インタプリタの場合
- 該当オブジェクトについて、どんな関数が用意されているかを調べる
>>> temp = 'hoge'
>>> temp.
temp.__add__( temp.__rmod__( temp.istitle(
temp.__class__( temp.__rmul__( temp.isupper(
temp.__contains__( temp.__setattr__( temp.join(
temp.__delattr__( temp.__sizeof__( temp.ljust(
temp.__dir__( temp.__str__( temp.lower(
temp.__doc__ temp.__subclasshook__( temp.lstrip(
temp.__eq__( temp.capitalize( temp.maketrans(
temp.__format__( temp.casefold( temp.partition(
temp.__ge__( temp.center( temp.replace(
temp.__getattribute__( temp.count( temp.rfind(
temp.__getitem__( temp.encode( temp.rindex(
temp.__getnewargs__( temp.endswith( temp.rjust(
temp.__gt__( temp.expandtabs( temp.rpartition(
temp.__hash__( temp.find( temp.rsplit(
temp.__init__( temp.format( temp.rstrip(
temp.__iter__( temp.format_map( temp.split(
temp.__le__( temp.index( temp.splitlines(
temp.__len__( temp.isalnum( temp.startswith(
temp.__lt__( temp.isalpha( temp.strip(
temp.__mod__( temp.isdecimal( temp.swapcase(
temp.__mul__( temp.isdigit( temp.title(
temp.__ne__( temp.isidentifier( temp.translate(
temp.__new__( temp.islower( temp.upper(
temp.__reduce__( temp.isnumeric( temp.zfill(
temp.__reduce_ex__( temp.isprintable(
temp.__repr__( temp.isspace(
# 例えば「temp.capitalize(」があるらしい。
# どんな関数なのか調べてみるにはヘルプを使おう。
>>> help(temp.capitalize)
Help on built-in function capitalize:
capitalize(...) method of builtins.str instance
S.capitalize() -> str
Return a capitalized version of S, i.e. make the first character
have upper case and the rest lower case.
- 主な調べ方3-2:
- VS Code, PyCharmの場合
- ファイル編集中に「変数名.」まで記述した時点で、その変数に保存しているオブジェクトが利用できる関数等の一覧が簡易表示される。
- VS Code, PyCharmの場合
- 主な調べ方4: google
- エラー文等の出力を調べる際には、文全体をダブルクォートで囲うとその文を含むページを検索できる。
>>> hoge
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'hoge' is not defined- 検索フレーズ1:"NameError: name 'hoge' is not defined"
- ここで、「hoge」は一般的なメッセージではなく、今回の手元のコードに特化した話。こういう場合は次のように2つに分けて検索するのも良い。
- 検索フレーズ2:"NameError: name" "is not defined"