Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
importを理解したかった
Search
tera
February 20, 2017
660
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
importを理解したかった
平成29年2月18日(土): 第四回 帰ってきたhojiroLT にて話しました未完のスライドです
tera
February 20, 2017
More Decks by tera
See All by tera
ピッてやるとシュッてなるやつを作った話
tera
0
260
Pythonを読む
tera
0
230
RustでShell作ろう
tera
0
950
HTTPをしゃべろう~Firefoxになるために~
tera
0
250
Featured
See All Featured
Accessibility Awareness
sabderemane
1
150
The agentic SEO stack - context over prompts
schlessera
0
840
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
1
2.1k
A Soul's Torment
seathinner
6
3k
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
270
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
62
55k
Practical Orchestrator
shlominoach
191
11k
The Art of Programming - Codeland 2020
erikaheidi
57
14k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
380
A Tale of Four Properties
chriscoyier
163
24k
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
400
Transcript
importを 理解できなかった話 @tera_3939
自己紹介 • Twitter:@tera_3939 • 手書きレポート・正弦波学科 • 許さねえぞ
アジェンダ • 謝罪 • importについて • __import__()が行う処理 • hook
アジェンダ • 謝罪 • importについて • __import__()が行う処理 • hook
スライドができませんでした
理由:怠惰
importとは • あるモジュールのコードから他のモジュールのコードをイン ポートすること • import math • from math
import pi • 他の言語だとuseとかusingとか
importの方法 • import文を使う • importlib.import_module()を使う • __import__()を使う(非推奨)
importの方法 • import文を使う ← これにちょっかいする • importlib.import_module()を使う • __import__()を使う(非推奨)
import文の処理 • 指定されたモジュール名を検索する • 適切な引数で__import__()を呼び出す • 見つかったら指定された名前にそのモジュールを束縛する • import math
-> mathという名前にmathを束縛 • import math as m -> mという名前にmathを束縛
import文の処理 • 指定されたモジュール名を検索する • 適切な引数で__import__()を呼び出す • 見つかったら指定された名前にそのモジュールを束縛する • import math
-> mathという名前にmathを束縛 • import math as m -> mという名前にmathを束縛
__import__()の処理 • sys.modulesを検索する • すでに読み込まれているモジュールのキャッシュ • 見つからなければimport機構が起動する • 有る場合はそのモジュールのmodule specを返す
• module specはモジュールのロードに使われるオブジェクト • 無い場合はImportErrorになる
import機構 • ファインダー, ローダーと呼ばれるオブジェクトが使用される • ファインダー • モジュールが見つかった場合module specを返す •
ローダー • モジュールをロードする • module specのloader属性に詰め込まれる
importをhookする • sys.meta_pathに自作ファインダーを加える • sys.path_hooksに自作ファインダーを加える • sys.path内のパスを処理する際に使用される
sys.meta_path • メタパス・ファインダー・オブジェクトのリスト • このオブジェクトが持つメソッドfind_spec()を順に呼び出す • モジュールが見つかればmodule specを返す • 見つからなければNoneを返す
おしまい • ごめんなさい…… • そのうちブログにまとめます……