PROPOSAL: ban explicit class constructors in SymPy

25 views
Skip to first unread message

Francesco Bonazzi

unread,
Nov 8, 2019, 5:21:04 PM11/8/19
to sympy
Constructors are being overloaded many times in SymPy.

The way a constructor is supposed to be written is quite standard, but there are so many ways they are being written and mis-written.

Should we rather replace __new__ with something different (like, for example, _eval_new?)

Advantages:
  • external control over evaluate=True/False,
  • standardized way of creating properties and storing variables,
  • automatic sympification for all args,
  • type checks at construction can be easily introduced.

Aaron Meurer

unread,
Nov 8, 2019, 5:36:16 PM11/8/19
to sympy
This is related to the discussion here
https://github.com/sympy/sympy/issues/17280.

I like the idea. The implementation of __new__ is often quite
confusing, and it isn't always clear if you are doing it correctly.
That's not to mention that most people aren't used to using __new__ in
Python. We already do basically this for Function subclasses, and it
works quite well.

I would start by consolidating the __new__ logic and introducing the
new method (maybe we should reuse the eval() name from Function?).
Then once we have that working, we can look at adding new automatic
behavior to __new__ like the things you mentioned.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/7270b2d3-f3d6-4f5e-98c8-dec79154f88a%40googlegroups.com.

Oscar Benjamin

unread,
Nov 9, 2019, 7:20:01 AM11/9/19
to sympy
I do plan to do this after the next release. There are some details to
work through in making something that works for all of Basic.

The Function model with eval works well but I'm not sure that it would
work for everything else. I think that we need to move the logic that
is currently in __new__ methods into an instance method so that a full
instance is created and then doit is called. The problem with
Function.eval is that it is a class method so it isn't easy for
subclasses to override its behaviour without completely rewriting the
method.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAKgW%3D6L1DPdnGbixKkiReGWfutUWiq%2Btxm0v48nTNX83dLPUjQ%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages