On 05/18/2015 04:12 PM, Amit Saha wrote: > On Mon, May 18, 2015 at 11:56 PM, Carsten Knoll <carstenkn...@gmx.de> wrote: >> I want to equip an Symbol with an additional attribute to store some >> specific information right in place. >> >> For 'normal' Python classes it is no problem to dynamically create an >> additional attribute for an already existing instance. >> >> However, for sympy Symbols if I try >> >> x = Symbol('x') >> x.k = 0 >> >> I get the error >> >> AttributeError: 'Symbol' object has no attribute 'k' >> >> Is there any possibility to equip the Symbol x with this attribute? >> >> I already tried this workarround: >> >> class ExtendedSymbol(sp.Symbol): >> pass >> >> >> x = ExtendendSymbol('x') >> >> x.k = 0 # no problem here >> >> >> However then I have the following unwanted behavior: >> >> x2 = sp.Symbol('x') >> x2 == x # -> False > > Not sure if it is possible for your situation, but may be you could > overrride the __eq__ method in your ExtendedSymbol class to call the > base class's method? >
I also thought in that direction, but how would that look like? I mean overriding a method just to call the respective method of the base class should be the same as not overiding the method at all, or not? I then thought on mirroring the Symbol inside and piping all external calls (including calls to '__eq__') to the wrapped Symbol instance and fetching only AttributeErrors outside. But this seems to complicated. The cleanest way would be to somehow dynamically highjack the SymbolClass itself. Equipping that class with new methods or class variables is no problem but adding instance attributes is not possible. Not sure where this behavior is implemented. -- 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+unsubscr...@googlegroups.com. To post to this group, send email to sympy@googlegroups.com. Visit this group at http://groups.google.com/group/sympy. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/5559FBBD.3020000%40gmx.de. For more options, visit https://groups.google.com/d/optout.