Using __slots__ in Sage

35 views
Skip to first unread message

Trevor Karn

unread,
Mar 28, 2022, 4:22:25 PM3/28/22
to sage-devel
Hi all,

I have just learned about using __slots__ for Python classes. It seems interesting to me and I am excited to see that it seems to be faster and require less memory. I don't have a specific use case in mind yet, but is there anything to be careful about in Sage where using __slots__ might break something?

Thanks,
Trevor

Travis Scrimshaw

unread,
Mar 29, 2022, 1:51:26 AM3/29/22
to sage-devel
Hi Trevor,
   IIRC, yes, and lots. It means you no longer have a __dict__ to work with, which affects @cached_method, pickling, dynamically adding attributes, etc. Not only could using __slots__ in Python classes have far reaching effects within the Sage library, it can affect user's code in the wild. Granted, the latter issue is unlikely to effect the more casual users (and I think happens when we Cythonize something). Subsequently it should be restricted to the places for classes used in tight loops, but then you probably should just fully Cythonize things.

Best,
Travis
Reply all
Reply to author
Forward
0 new messages