For this you want to use __classcall_private__ as otherwise you would 
likely end up in an infinite loop when you try to construct the subclass. 
There are lots of examples of this in the Sage library code.

Best,
Travis


On Sunday, May 15, 2022 at 5:42:49 AM UTC+9 Eric Gourgoulhon wrote:

> Hi, 
>
> Le samedi 14 mai 2022 à 00:07:02 UTC+2, David Roe a écrit :
>
>> I think the following should work:
>>
>> class MyObject:
>>     def __classcall__(cls, arg):
>>          if isinstance(arg, special):
>>              return typecall(MyObject_specific_case, arg)
>>          else:
>>              return typecall(MyObject, arg)
>>
>> plus the same __init__ you had before.  I haven't checked it though....
>> David
>>               
>>
>
> An alternative is to use __classcall_private__
> For an example, see the class EuclideanSpace in
> src/sage/manifolds/differentiable/examples/euclidean.py 
> <https://github.com/sagemath/sage/blob/develop/src/sage/manifolds/differentiable/examples/euclidean.py>
>
> EuclideanSpace(n) actually returns an instance of the subclass 
> EuclideanPlane if n = 2 or of the subclass Euclidean3dimSpace if n = 3.
>
> Best wishes,
>
> Eric.
>

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sage-devel/8280058a-377a-43ca-baab-1b96f5257a05n%40googlegroups.com.

Reply via email to