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
みんなのPython勉強会_LT枠_10_12__5分で理解するディスクリプタ.pdf
Search
Yusuke Tamura
October 12, 2023
1.2k
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
みんなのPython勉強会_LT枠_10_12__5分で理解するディスクリプタ.pdf
stapy#98のLTで発表した内容になります
https://startpython.connpass.com/event/296755/
Yusuke Tamura
October 12, 2023
More Decks by Yusuke Tamura
See All by Yusuke Tamura
Developing UI Components Integrating Radix Primitives with Custom CSS
tamtam0423
0
110
Which should be designed first, the data model or the domain model
tamtam0423
0
250
みんなのPython勉強会#100 LT枠 参照カウントから見るPythonのメモリ管理
tamtam0423
5
1.6k
Understanding Metaclass from Type
tamtam0423
0
91
めぐろLT_ Pythonの弱参照(weakref)を使えば、世界はもっと良くなる
tamtam0423
0
860
みんなのPython勉強会#96 LT枠
tamtam0423
0
240
NoCodeツールでWebHookからDWHへのデータの転送処理を爆速で実装した話
tamtam0423
0
420
Featured
See All Featured
The Mindset for Success: Future Career Progression
greggifford
PRO
0
520
Fashionably flexible responsive web design (full day workshop)
malarkey
409
67k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.9k
Docker and Python
trallard
47
4.2k
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
330
The agentic SEO stack - context over prompts
schlessera
0
940
Claude Code のすすめ
schroneko
67
230k
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
540
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
Amusing Abliteration
ianozsvald
1
320
Six Lessons from altMBA
skipperchong
29
4.5k
Raft: Consensus for Rubyists
vanstee
142
7.7k
Transcript
@ 2023 Novasell, Inc. All Rights Reserved. 5分で理解するPythonのディスクリプタ 2023/10/12 みんなのPython勉強会
LT枠 ノバセル株式会社 ⽥村湧介(tamtam) 1
@ 2023 Novasell, Inc. All Rights Reserved. 自己紹介 GitHub: @tamtam-fitness,
X: @TamtamFitness, Zenn: Tamtam たむたむ(tamtam)といいます! ラクスル/ノバセルの新卒2年⽬です💪 FastAPIの翻訳/記事の執筆を定期的にしてます🐍
@ 2023 Novasell, Inc. All Rights Reserved. 3 ディスクリプタとは __get__(),
__set__(), __delete__() を1つ以上定義しているオブジェクト 引⽤: descriptor --- Python 3.12.0 ⽤語集 __get__(), __set__(), あるいは __delete__() を定義しているオブジェクトです。あるクラス属性がデスクリプタであるとき、 属性探索によって、束縛されている特別な動作が呼び出されます。 通常、get,set,deleteのために a.b と書くと、 a のクラス辞書内でオブジェクト b を検索しますが、 b がデスクリプタであ ればそれぞれのデスクリプタメソッドが呼び出されます。
@ 2023 Novasell, Inc. All Rights Reserved. 4 早速試してみる①
@ 2023 Novasell, Inc. All Rights Reserved. 5 ディスクリプタプロトコル(__get__など)が クラスレベルの属性アクセスに適⽤されるようにする
早速試してみる①
@ 2023 Novasell, Inc. All Rights Reserved. 6 早速試してみる①: 補⾜
Pythonはディクショナリをユーザー定義オブジェクトの クラス変数やインスタンス変数を管理する際に⽤いる。
@ 2023 Novasell, Inc. All Rights Reserved. 7 ディスクリプタの__get__や__set__メソッドが呼び出されるようになるには ディスクリプタオブジェクトがクラスのディクショナリに存在する必要がある。
早速試してみる①: 補⾜
@ 2023 Novasell, Inc. All Rights Reserved. 8 早速試してみる① MyClassの属性を呼び出した際に
ディスクリプタプロトコル(__get__など)が呼ばれる
@ 2023 Novasell, Inc. All Rights Reserved. 9 class Descriptor:
def __init__(self, name): self.name = name def __get__(self, instance, cls): print(f"{self.name}: __get__") def __set__(self, instance, value): print(f"{self.name}:__set__ {value}") def __delete__(self, instance): print(f"{self.name}:__delete__") obj = MyClass() obj.attribute obj.attribute = "y" del obj.attribute 注意: 第⼀引数で使われるselfはディスクリプタ⾃⾝、第⼆引数のinstanceは操作されるオブジェクト。 早速試してみる①
@ 2023 Novasell, Inc. All Rights Reserved. 10 早速試してみる② ディスクリプタはインスタンスのディレクトリに
同じ名前の属性があった場合はオーバーライドして実⾏する
@ 2023 Novasell, Inc. All Rights Reserved. 11 応⽤例: ORMで使うモデルの属性の実装
@ 2023 Novasell, Inc. All Rights Reserved. 12 応⽤例: ORMで使うモデルの属性の実装
@ 2023 Novasell, Inc. All Rights Reserved. 13 ディスクリプタはいたる所で使われている! e.g)
instance method, @classmethod, @staticmethod, @property, __slots__ の実装 ディスクリプタ HowTo ガイド
@ 2023 Novasell, Inc. All Rights Reserved. 14 例:インスタンスメソッドの呼び出し
@ 2023 Novasell, Inc. All Rights Reserved. 15 例:インスタンスメソッドの呼び出し
@ 2023 Novasell, Inc. All Rights Reserved. 16 例:インスタンスメソッドの呼び出し
@ 2023 Novasell, Inc. All Rights Reserved. 17 例:プロパティ
@ 2023 Novasell, Inc. All Rights Reserved. 18 例:プロパティ
@ 2023 Novasell, Inc. All Rights Reserved. 19 参考 •
Advanced Python Mastery - Section4: Inside Python Objects • デスクリプタ HowTo ガイド • Effective Python - 4章 メタクラスと属性 • Python のディスクリプタ __get__ メソッドってなに? | ⺠主主義に乾杯
@ 2023 Novasell, Inc. All Rights Reserved. 20 ご静聴ありがとうございました!