Share
Embed
【初心者向け】ググらずに!? ターミナル上で Pythonオブジェクトを調べよう
Python Tips LT会 - vol.3 #pythontipslt
にて発表したスライドです。
Yumihiki
May 25, 2022
Transcript
39 slides
-
Yumihiki Python Tips LT会 - vol.3 #pythontipslt 【初心者向け】ググらずに!? ターミナル上で
Pythonオブジェクトを調べよう -
自己紹介 Yumihiki / Tatsuya NIBUNO パーソルテクノロジースタッフ株式会社 GitHub: @Yumihiki Twitter: @YumihikiDev
2020年2月〜実務未経験から転職 / エンジニア歴3年目 エセ関西人 -
対象者 • Pythonビギナー • Pythonチュートリアルやったことがない(=実は私も) • 基礎文法(list, dict)などの概念は知っている(知らなくても全然聞けます) • 対話モードを使ったことがない人
• 知っている方はドヤ顔で説明するのを温かい目で見てください • 実行環境はMacにて実施しています ◦ Windowsでも同様のことは出来るはず • 元ネタは以下リンクにあります(スライドは後で公開します!) ◦ 一般 Python FAQ — Python 3.10.4 ドキュメント • 及びその周辺技術?についての解説アレコレ -
本題 tatsuya@MyMBP ~ % python3 Python 3.8.1 (v3.8.1:1b293b6006, Dec 18
2019, 14:08:53) [Clang 6.0 (clang-600.0.57)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> [i for i in dir([]) if '__' not in i] ['append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] >>> 🔴 🟠 🟢 -
本題 tatsuya@MyMBP ~ % python3 Python 3.8.1 (v3.8.1:1b293b6006, Dec 18
2019, 14:08:53) [Clang 6.0 (clang-600.0.57)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> [i for i in dir([]) if '__' not in i] ['append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] >>> 🔴 🟠 🟢 -
本題 tatsuya@MyMBP ~ % python3 Python 3.8.1 (v3.8.1:1b293b6006, Dec 18
2019, 14:08:53) [Clang 6.0 (clang-600.0.57)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> [i for i in dir([]) if '__' not in i] ['append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] >>> 🔴 🟠 🟢 -
本題 tatsuya@MyMBP ~ % python3 Python 3.8.1 (v3.8.1:1b293b6006, Dec 18
2019, 14:08:53) [Clang 6.0 (clang-600.0.57)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> [i for i in dir([]) if '__' not in i] ['append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] >>> 🔴 🟠 🟢 -
本題 tatsuya@MyMBP ~ % python3 Python 3.8.1 (v3.8.1:1b293b6006, Dec 18
2019, 14:08:53) [Clang 6.0 (clang-600.0.57)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> [i for i in dir([]) if '__' not in i] ['append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] >>> 🔴 🟠 🟢 -
本題 >>> help('list.append') Help on method_descriptor in list: list.append =
append(self, object, /) Append object to the end of the list. (END) >>> >>> help('list.append') -
本題 >>> help('list.append') Help on method_descriptor in list: list.append =
append(self, object, /) Append object to the end of the list. (END) >>> >>> help('list.append') helpウィンドウに 切り替わる 終了時は q を叩く -
本題 >>> help('list.append') Help on method_descriptor in list: list.append =
append(self, object, /) Append object to the end of the list. >>> help('list.append') >>> -
解説 Q. python3 ってなんなん? A. Pythonインタプリタを起動するコマンド 2. Python インタプリタを使う 対話モードと呼ばれる状態になる。
>>> と表示されている場所にコードを書くことで実行できる (for文などのように続く場合は … )。 今回のような簡単なコードの検証に使える。 exit() or Ctrl + D で終了できる。 -
解説 Q. [i for i in dir([]) if '__' not
in i] ってなんなん?ワイの知ってるfor文ちゃうで? A. リスト(の)内包表記 です。 5. データ構造 — Python 3.10.4 ドキュメント for文でリストを回してif文で条件を書いて何かする...ケースを1行で書ける。 if文が無いケースも記述することができる。 ex: リスト内の文字を全て小文字に操作する 今回のケースだと内包表記を使わない場合、次のようなコードになる。 -
>>> not_special_attributes = [] >>> for i in dir([]): …
if '__' not in i: … not_special_attributes.append(i) … not_special_attributes # このままでも良いけど、1行でスッキリと書くことができる = 内包表記 本題 🔴 🟠 🟢 -
>>> not_special_attributes = [] >>> for i in dir([]): …
if '__' not in i: … not_special_attributes.append(i) … not_special_attributes # このままでも良いけど、1行でスッキリと書くことができる = 内包表記 本題 🔴 🟠 🟢 スッキリと 言っても ややこしくない? -
>>> not_special_attributes = [] >>> for i in dir([]): …
if '__' not in i: … not_special_attributes.append(i) … >>> not_special_attributes >>> [i for i in dir([]) if '__' not in i] 本題 🔴 🟠 🟢 動きを1つずつ 見ていくと 混乱しないかも🦆? -
解説 Q. if '__' not in i の'__'ってなにしてんの? 人の顔? A.
特殊メソッドを省いていました。使わない場合以下のようになります。 >>> [i for i in dir([])] # dir([]) と同じ結果 ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] 🔴 🟠 🟢 -
解説 Q. さらっと流してたけどdirってなんや? A. dir([object]): オブジェクトを引数に渡すことでオブジェクトの有効な属性の listを 返してくれる(正確には返そうと試みてくれる?) 組み込み関数 —
Python 3.10.4 ドキュメント listが返却されることでfor文で処理できる ( =特殊メソッド __iter__ を実装したオブジェクトはイテラブルなオブジェクト これを実装しているとfor文で処理できる) ※Pythonは関数、メソッド、文字列などは全てオブジェクト -
解説 Q. helpについても教えてくれや!今回のキモちゃうんか!?help me!! A. help([object]): オブジェクトを引数に渡すことで そのオブジェクトに関するヘルプページを生成してくれる オブジェクトではなく‘list.append’のような文字列でも検索・表示してくれる 組み込み関数
— Python 3.10.4 ドキュメント ※Pythonは関数、メソッド、文字列などは全てオブジェクト (大事なことなので2回言うた) -
解説 🔴 🟠 🟢 Help on list object: class list(object)
| list(iterable=(), /) | | Built-in mutable sequence. | | If no argument is given, the constructor creates a new empty list. | The argument must be an iterable if specified. | | Methods defined here: | | __add__(self, value, /) | Return self+value. | -
解説 >>> help(l.append) 🔴 🟠 🟢 Help on method_descriptor in
list: list.append = append(self, object, /) Append object to the end of the list. (END)