On 05/18/2015 04:48 PM, Carsten Knoll wrote:
> 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.

> 
> 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.
> 


If someone is interested: I found an (ugly) workaround:


# because sympy does not allow to dynamically attach attributes
# to symbols we set up our own infrastructure for storing them


import sympy as sp
sp._attribute_store = {}


def new_setattr(self, name, value):
    try:
        self.__orig_setattr__(name, value)
    except AttributeError:
        sp._attribute_store[(self, name)] = value


def new_getattr(self, name):
    try:
        res = self.__getattribute__(name)
    except AttributeError, AE:
        try:
            res = sp._attribute_store[(self, name)]
        except KeyError:
            # raise the original AttributeError
            raise AE
    return res


sp.Symbol.__orig_setattr__ = sp.Symbol.__setattr__
sp.Symbol.__setattr__ = new_setattr

sp.Symbol.__getattr__ = new_getattr



Still, I would prefer to store the attribute directly 'inside'  the
symbol instance but at least in the rest of my code this solution feel
like it were like that.

Carsten.

-- 
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/555A1D0B.2090905%40gmx.de.
For more options, visit https://groups.google.com/d/optout.

Reply via email to