arcade icon indicating copy to clipboard operation
arcade copied to clipboard

Sprite has a __dict__, despite its __slots__, because of PymunkMixin

Open sjrd opened this issue 7 months ago • 2 comments

The class Sprite defines __slots__ at https://github.com/pythonarcade/arcade/blob/580dd21603c662502326678a2edcde5a72a42f3e/arcade/sprite/sprite.py#L48-L62 However, it still gets a __dict__, because it extends PymunkMixin, which does not define __slots__.

It's not possible to add __slots__ = ('pymunk',) to PymunkMixin, because a class cannot extend two classes that define non-empty __slots__.

I suggest adding an empty __slots__ = () to PymunkMixin, and add 'pymunk' to the slots of Sprite. It's not pretty, but it seems like the least disruptive change.

We can observe the difference by looking at the __dict__ property:

basic = BasicSprite('whatever')
print(basic.__dict__) # AttributeErrror, good!

sprite = Sprite('whatever')
print(sprite.__dict__) # displays a dict, bad

That can form the basis of a unit test.

sjrd avatar May 13 '25 11:05 sjrd

This was done deliberately because we didn't want to break backwards compatibility supporting dynamic members in Sprite.

einarf avatar May 15 '25 11:05 einarf

Ah I see. That makes sense. But wouldn't it be clearer to then list "__dict__" explicitly in the __slots__, rather than relying on the fact there happens to be a mixin without __slots__?

sjrd avatar May 15 '25 11:05 sjrd