Am 19.05.2015 um 00:44 schrieb Carsten Knoll:
>
>> That happens because we use __slots__ for performance reasons.
>
> I was not aware of the __slots__ mechanism. Now I know.
>
>> However, storing attributes in that way is a bad idea in general,
>> because you risk name conflicts with future versions of SymPy. (That's
>> also the reason why using subclasses to extend a library tends to break
>> over time.)
>
> OK.
>
> On the other hand syntax looks much cleaner
I prefer "maintainable" over "looks nice" any day.
However, that's just my professional bias because in software
engineering, maintenance dwarves all other concerns.
>> Alternative 1: You could set up a dict with a Symbol-to-attributes
>> mapping: Symbols are hashable (that's a guaranteed property).
>
> Thats what I meanwhile did (see my other message on this thread). In
> addition I redefine __setattr__ and __getatrr__ of the Symbol class to
> hide the dictionary access behind the scene.
>
> This feels somehow strange but it works. Lets see how long.
Expect some loss of performance with the __getattr__/__setattr__ overrides.
Not sure how much - could be large, could be negligible.
>> Alternative 2: We could add a `userdict` attribute in Symbol, so that
>> SymPy users can store additional attributes there.
>
> This would be nice from my point of view. But wouldnt this cause
> performance issues? As I understand, __slots__ is used to prevent every
> Symbol instance from having a dict. But introducing a `userdict` would
> negate this effort.
The mere existence of a dict does not impact SymPy much, it's a one-time
overhead during symbol creation. Unless you create a gazillion of
symbols, this should be negligible.
Still, I have seen that done (e.g. in JDK's Swing), and it comes with
issues.
E.g. two different third-party libraries fighting over the namespace
inside the `userdict`.
Also, all user attributes now being x.userdict.attr instead of x.attr.
Also, yet another detail to know about SymPy (as usual, there are alread
too many of them; it's adding to the learning curve).
Also, the constant temptation to use it for SymPy itself instead of
properly refactoring things.
An external dictionary isn't ideal but better than this, at least from
the maintainer's perspective.
>> Maybe we can give you better help if you describe your use case in some
>> more detail.
>
> At some point I create symbols from which I know, that they are
> derivatives and I want to store their order. Using the classes Function
> and Derivative is no real option because I want the symbols to behave as
> symbols on most occasions.
Ah. This sounds like a design problem in Derivative to me that we should
fix.
Can you post what you did, what you'd have needed to get out, and what
SymPy gave you?
> In general, I think there are situations, where it might be useful to
> store some algorithm-specific extra information to some symbols.
For algorithms of SymPy itself, I think the algorithms should be fixed :-)
It would be useful for coding external algorithms, but it does come with
ramifications (most importantly the potential for namespace conflicts);
from a maintenance perpective, it's far better if such an algorithm does
its own data management.