I'm having a tricky problem in the game I'm working on. I'm using Pygame to develop it. I happen to be one of those developers who never uses the default__dict__
object variable; I always define __slots__
to clarify the variables an object can have (I have a classmethod that reads the slots to determine the variables needed from a config file).
Anyway, I just realized that this effort isn't working in some of my classes; they still have a __dict__
variable and can have arbitrary attributes assigned to, even though they explicitly define their __slots__
. I think this is because they are inheriting from pygame.sprite.Sprite, which has a __dict__
. If this is the case, how do I suppress creation of this dict? (I though explicitly defining __slots__
was supposed to) Or could I be mistaken about the cause? Thanks for any insight; it's hard to find information about this particular problem via searches.
The only way to suppress arbitrary attributes and the __dict__
container of them, is to use __slots__
as you are and inherit from a class that does the same. A subclass of a class that has a __dict__
will always have a __dict__
. The only way around it is to not inherit from this class (but, for example, use composition instead.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With