Skip to content
Discussion options

You must be logged in to vote

Good question, slots can definitely help in some situations, but it’s not always worth using. By default, Python gives every object a dynamic dict, which allows you to add attributes at runtime, but that also uses more memory. When you define slots, you tell Python exactly which attributes an object can have, and it skips creating the dict, which can lead to significant memory savings, especially if you’re creating thousands or millions of instances. That said, there are tradeoffs. You lose the ability to add new attributes unless you explicitly include dict in the slots, and using slots can make inheritance a bit trickier, especially when combining it with other classes that also define …

Replies: 4 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by JacobZimmerman2029
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question Ask and answer questions about GitHub features and usage Programming Help Discussions around programming languages, open source and software development
5 participants