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
80
めぐろLT_ Pythonの弱参照(weakref)を使えば、世界はもっと良くなる
tamtam0423
0
840
みんなのPython勉強会#96 LT枠
tamtam0423
0
240
NoCodeツールでWebHookからDWHへのデータの転送処理を爆速で実装した話
tamtam0423
0
400
Featured
See All Featured
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
300
The Spectacular Lies of Maps
axbom
PRO
1
860
Discover your Explorer Soul
emna__ayadi
2
1.2k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
2.9k
RailsConf 2023
tenderlove
30
1.5k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
220
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
0
450
Marketing to machines
jonoalderson
1
5.6k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.8k
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
2
610
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.4k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
510
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 ご静聴ありがとうございました!