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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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
92
めぐろLT_ Pythonの弱参照(weakref)を使えば、世界はもっと良くなる
tamtam0423
0
860
みんなのPython勉強会#96 LT枠
tamtam0423
0
240
NoCodeツールでWebHookからDWHへのデータの転送処理を爆速で実装した話
tamtam0423
0
420
Featured
See All Featured
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
35
2.9k
Git: the NoSQL Database
bkeepers
PRO
432
67k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
520
WENDY [Excerpt]
tessaabrams
14
40k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
2
930
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
580
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
3
4.3k
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.6k
Faster Mobile Websites
deanohume
310
32k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6.1k
Darren the Foodie - Storyboard
khoart
PRO
4
4k
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 ご静聴ありがとうございました!