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
95
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
78
めぐろLT_ Pythonの弱参照(weakref)を使えば、世界はもっと良くなる
tamtam0423
0
840
みんなのPython勉強会#96 LT枠
tamtam0423
0
240
NoCodeツールでWebHookからDWHへのデータの転送処理を爆速で実装した話
tamtam0423
0
400
Featured
See All Featured
How Software Deployment tools have changed in the past 20 years
geshan
0
34k
Embracing the Ebb and Flow
colly
88
5.1k
WENDY [Excerpt]
tessaabrams
11
38k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.1k
Making Projects Easy
brettharned
120
6.7k
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
Ruling the World: When Life Gets Gamed
codingconduct
0
260
Building Adaptive Systems
keathley
44
3.1k
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
400
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.3k
The #1 spot is gone: here's how to win anyway
tamaranovitovic
2
1.1k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
62
54k
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 ご静聴ありがとうございました!