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
みんなの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
99
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
83
めぐろLT_ Pythonの弱参照(weakref)を使えば、世界はもっと良くなる
tamtam0423
0
850
みんなのPython勉強会#96 LT枠
tamtam0423
0
240
NoCodeツールでWebHookからDWHへのデータの転送処理を爆速で実装した話
tamtam0423
0
410
Featured
See All Featured
RailsConf 2023
tenderlove
30
1.5k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
400
Amusing Abliteration
ianozsvald
1
240
Typedesign – Prime Four
hannesfritz
42
3.1k
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
610
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
480
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
270
Automating Front-end Workflow
addyosmani
1370
210k
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
470
Java REST API Framework Comparison - PWX 2021
mraible
34
9.6k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
930
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 ご静聴ありがとうございました!