On Mon, Nov 17, 2008 at 10:23 PM, Drake <[EMAIL PROTECTED]> wrote: > > Hi Ondrej, > >> > I'm looking into the AssocOp code to implement the functions that will >> > flatten a longer expression, and I have a question. Why does SymPy use >> > this structure everywhere: >> >> > class Basic(...): >> > is_Atom = False >> >> > class AssocOp(...): >> > @classmethod >> > def identity(cls): >> > if cls is Mul: return S.One >> >> ^^ do you mean why it references the Mul class here? Or is your >> question about something else? > > Let me clarify what I am asking. Take the example of AssocOp. Mul is a > derived class from AssocOp, yet the AssocOp "identity" method asks if > it is an instance of Mul. It seems to me that a base class shouldn't > have knowledge about its derived classes. Derived classes, on the > other hand, should know what their base classes are.
I think some of this type of thing is unavoidable as sympy expressions can be arbitrarily nested (especially Mul). > Similarly for classes derived from Basic. Atom is derived from Basic, > yet Basic has a property called "is_Atom". This class entanglement > seems a bit cumbersome to me. Since I haven't read "Design Patterns", > I was just wondering if there's a proverbial method to this > madness :). As Ondrej mentioned, these things have bothered me greatly. There is a thread on the list in the last on this topic. I will try to summarize my understanding of the is_* stuff. * The simplest and nicest thing from a design standpoint is to get rid of the is_* attributes and use isinstance to detect when you have an instance of a certain class. * However, isinstance is slower than attribute lookup. * The is_* attributes were created so that the core code in sympy could have a fast path for checking the types of the objects that it gets. I am assuming that performance tests were done and that there really was a net performance gain. By the way, how much of a difference did it make on average? * I still don't like this design, but my current approach is to basically ignore that is_* attributes unless I am doing something deep in the core of sympy. This seems to work well. My own preference in the long term would be to 1) move the core of sympy to cython to take care off all the performance issues and 2) clean up all of these performance hacks to make the design cleaner, more maintainable and more hack friendly. Cheers, Brian > Chris > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sympy" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sympy?hl=en -~----------~----~----~----~------~----~------~--~---
