Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
importを理解したかった
Search
tera
February 20, 2017
670
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
270
Pythonを読む
tera
0
230
RustでShell作ろう
tera
0
960
HTTPをしゃべろう~Firefoxになるために~
tera
0
260
Featured
See All Featured
The SEO identity crisis: Don't let AI make you average
varn
0
560
Skip the Path - Find Your Career Trail
mkilby
1
240
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
2k
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
260
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.8k
Information Architects: The Missing Link in Design Systems
soysaucechin
1
1.2k
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
550
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.6k
Testing 201, or: Great Expectations
jmmastey
46
8.3k
The SEO Collaboration Effect
kristinabergwall1
1
570
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.5k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
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を返す
おしまい • ごめんなさい…… • そのうちブログにまとめます……