dataclass は、Pythonで主にデータを格納するためのクラスで、C言語などでは構造体に相当するようなデータ構造を、かんたんに定義できるようになっています。 たとえば、次の Person は、名前と年齢を格納するdataclassです。 from datacl (その1) パターンマッチ ¦ (その2) with文のネスト ¦ (その3) エラーメッセージの改善 ¦ (その4) | 演算子によるユニオン型の指定 ¦ (その5) パラメータ仕様変数 ¦ (その6) 明示的な型エイリアス ¦ (その7) ユーザ定義型ガード ¦ (その8) OpenSSL 1.1.1が必須に ¦ (その9) zip()関数に引数 strict を追加 ¦ (その10) Dataclassでslotsが利用可能に ¦ (その11) その他の変更
通常、クラスのインスタンスは、メンバー変数の名前と値を __dict__ という名前の辞書オブジェクトに格納します。
>>> classFoo:… pass…>>> foo=Foo()>>> foo.attr1=“いぬ”>>> foo.attr2=“ねこ”>>>>>> print(foo.__dict__){‘attr1’: ‘いぬ’, ‘attr2’: ‘ねこ’}しかし、クラス定義中に __slots__ という名前で、使用する属性名のシーケンスを指定すると、__dict__ 辞書が作成されなくなります。
>>> classBar:… __slots__=(‘attr1’,‘attr2’)…>>> bar=Bar()>>> bar.attr1=“うし”>>> bar.attr2=“うま”>>> bar.__dict__Traceback (most recent call last): File “<stdin>”, line 1, in <module>AttributeError: ‘Bar’ object has no attribute ‘__dict__’
スロットのメリット¶
通常、クラスのインスタンスを一つ作成すると、__dict__ に使用する辞書が一つ、新しく作成されます。
辞書オブジェクトはパフォーマンスと柔軟性重視なデータ構造で、あらかじめ余計にメモリを確保して、データの追加や削除を高速に行えるような仕組みになっています。しかし、dataclass のように、決まった形式のデータを大量に保持するようなクラスでは、めったに属性の追加や削除は行いませんから、このために確保したメモリは無駄になってしまいます。
そこで、__slots__ を指定すると、インスタンスの属性値を辞書ではなく、スロットという配列形式で格納するようになり、辞書オブジェクトの作成に必要な処理時間と、メモリ使用量を削減できます。一方、__slots__ を使ったインスタンスで属性を参照する場合、数%程度速度が低下します。
dataclassのスロット¶
__slots__ 形式の dataclass は、次のように slots=Trueを指定して定義します。
fromdataclassesimportdataclass@dataclass(slots=True)classPerson2:name:strage:intperson1=Person2(name=“パーソン太郎”,age=20)person2=Person2(name=“パーソン次郎”,age=30)実際に実行時間とメモリ使用量を測定してみましょう。
__slots__ を使用しない場合は次のとおりです。
>>> importtime,tracemalloc>>> tracemalloc.start()>>> f=time.time()>>> persons=Person(str(i),i)foriinrange(1000000)>>> print(time.time()-f)2.6621129512786865>>> current,peak=tracemalloc.get_traced_memory()>>> print(current,peak)(243329878, 243330262)処理時間は2.7秒、メモリ使用量は243MB程ですね。
__slots__ を使用するとこんな感じになります。
>>> f=time.time()>>> persons=Person2(str(i),i)foriinrange(1000000)>>> print(time.time()-f)1.7276349544525146>>> current,peak=tracemalloc.get_traced_memory()>>> print(current,peak)(139330518, 139330902)処理時間は1.7秒、メモリ使用量は139MBまで削減できました。
また、__dict__ 辞書 を持たないインスタンスでは、あたらしい属性に値を代入できません。代入すると、次のようなエラーとなります。
>>> person=Person2(“名前”,“年齢”)>>> person.addr=“じゅうしょ"Traceback (most recent call last): File “<stdin>”, line 1, in <module>AttributeError: ‘Person2’ object has no attribute ‘addr’この例の Person のようなクラスでは動的にあたらしく属性を追加することはあまりありませんので、特に問題にはなりません。むしろ、属性名間違いによるBug防止に役立つでしょう。
スロットとは?スロットのメリットdataclassのスロット
luckyland slots review
Free Slots Guide 2026: Play Slots Free Live Demo Now
What Does Texas Holdem Flop Mean?
New Rules for WSOP Paradise: Electronic Devices and Real-Time Assistance
Wizard of Oz Slots Games on the App Store
LuckyLand Slots Social Casino Review Is LuckyLand Slots Legit?
How many slot machines does Scarlet Pearl have?
Download more similar LuckyLand Casino Real Money games/apps on PC
Best No Deposit Bonus Slots 2026: Free Spins & Real Money Wins
Which Slots Give Real Money? Top High-Payout Games
Does Tigrinho Pay on Signup? Truth Revealed
luckyland slots review
Download Luckyland Slots APK Latest v5.2.9 Free for Android
Free Spins on Registration: Best Casinos and Tips for 2026
Does Ruby Slots Really Pay Out? Honest Review 2025
Slot machine - Wikipedia
Apostas online e Casino Bónus Leon: Aposta grátis até 200€
Amazon.co.jp
Do slot games pay real money?
Wizard of oz slots free coins - Techyhigher
Casino bonus faqs
How to get coins on Wizard of Oz slots?
luckyland slots app
40 Burning Hot Slot - Free to Play Online Casino Demo Game
Wizard Of Oz Slots Free Credits 2021 Payment by bank transfer in online casinos – Hot Pepper Liberia
Where Is RubyPlay Headquarters?
2025 WSOP Explosion! New Formats + Huge Prizes Poker Players Must Not Miss It!
Bônus de poker sem depósito Bônus de $8 grátis 888poker
How to get free coins on House of Fun Slots?
Best Betting Sites with Free Bet on Registration in Brazil 2026
Real money betting FAQs
What are some famous roulette scandals and controversies throughout history?
Tesco shopper? Christmas delivery slots now open for some
luckyland slots app
What Is the $20 Method for Slot Machines?
Wizard Of OZ Slots Free Coins ( October 2024 ) - Daily Credits Updated - Fanatics Point
Philadelphia Eagles CB Bradley Roby to the Rescue in Slot for Philly - Sports Illustrated Philadelphia Eagles News Analysis and More
Juegos de Slots y Tragamonedas Online de Casino bplay
Is LuckyLand Casino Real? The Truth About Sweepstakes Slots
Top Real Money Slots: Win Big with Golden Buffalo Now!
Free Spins on Registration: Best Casinos and Tips for 2026
What is the most winnable slot machine?
Cory Zeidman Pleads Guilty to Sports Betting Fraud Scheme
Scatter Slots を入手 - Microsoft Store ja-JP
luckyland slots
Lucky Slots No Deposit Bonus
Plan Your Visit - Ruby Falls
Help - We regret to inform you that your IP address has been blocked
Wizard of Oz Free Slots Casino Télécharger gratuitement et rapidement nouvel Android APK - 40407
Wizard Of Oz Slots Free Credits 2021 Payment by bank transfer in online casinos – Hot Pepper Liberia
Thousands book slots to use Cornwall recycling centres
How to get more credits on Wizard of Oz slots?
How to Play Winning Jackpot Slots: 10 Proven Strategies to Hit the Big Win
WPT Global Partners with JP Poker Card Club & Casino in Ireland
Luckyland Casino Does Luckyland Slots Pay Real Money or Not?
luckyland slots app
LuckyLand Slots: A Casual Social Casino Guide
Play Free Slots Online Best Vegas Casino Slot Demos
Bonus Slot Guide
Worlds Biggest Slot Win: $42 Million Jackpot Shatters 20-Year Record
Common Misconceptions About Real-Money Online Slots in 2025
どのRAMスロットを使うべきか? CORSAIR
Bradley Roby Released by Saints After CB Was Involved in NFL Trade Rumors
Meet the Final Table of the 2024 EPT Barcelona Main Event
Wizard Of Oz Slots Free Coins – Guide to payment methods in online casinos Wag & Shine
LuckyLand Alternatives: Top Sweepstakes Casinos Like LuckyLand Slots