It's probably worth pointing out that __classcall_private__ is not a standard python facility. It looks like you need
class Foo(metaclass=ClasscallMetaclass): to make it work on Foo. On Saturday, 14 May 2022 at 20:21:48 UTC-7 Travis Scrimshaw wrote: > 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/a8bc696f-7887-41b2-8fe9-af3ac055eb71n%40googlegroups.com.