Is there a way to subclass set, with the binary operator returning the subclassed type, without redefining them ?
example :
class A(set):
pass
a = A([1,2,3]) & A([1,2,4])
a.__class__ == A # it's False, and I would like it to be true without redefining all operators
Note that this question : What is the correct (or best) way to subclass the Python set class, adding a new instance variable? is 10 years old and the provided answers are only relevant for python 2.x. This is why I asked another question concerning python 3.x (especially, python ≥ 3.8).
I believe the answer is "No".
As I skim the reference implementation it looks like all paths for
set_and()seem to ultimately return eitherNULLor the result of callingmake_new_set_basetype().That method is currently implemented as:
Thus without overriding
__and__()in your sub-class, what you are going to get back is aset()or afrozenset().