You almost never want to generate a symbol yourself (in library code) because you never know whether the user is using a the same name (pretty much the whole point of symbols are that they are "things" identified only by name).
So you have two options: 1. use Dummy 2. appropriately add additional information to the `args` of your class You are almost doing 2, but... ... is there a reason to use __new__ here. __init__ seems to be sufficient. Unless there is some magic involved, in you code you are not saving `coord_sys` in `args`, which is the actual reason that equality checks and caching are misbehaving. I think that if you are using __init__ `args` will be correctly populated automatically. On 25 July 2013 17:17, Prasoon Shukla <[email protected]> wrote: > I have subclassed Symbol, like so: > > class BaseScalar(Symbol): > """ > BaseScalar instances are used to express coordinate variables for > field. > Not to be instantiated by the user. > """ > def __new__(cls, name, coord_sys, position, **assumptions): > if (position not in ['1', '2', '3'] > and not isinstance(coord_sys, CoordSysRect)): > raise ValueError("Position of scalar not specified. \ > See `position` in docstring of `BaseScalar`") > if name is None: > name = 'Dummy_' + str(Dummy._count) > obj = Symbol.__new__(cls, name, **assumptions) > obj.coord_sys = coord_sys > return obj > > The problem is this: > An instance of this class is created with name 'x' (say). Fine. Everything > works as it should. Then, another instance of this class is created - again > with the name 'x'. The returned object is actually the same 'x' which was > made before. This is happening because __new__ of Symbol is cached - when > the new object is created, it returns the cached object from before. > > What can I do for to generate a new instance of the class instead of > returning the already created object? > > > P.S.: If you are wondering why I am creating the objects with the same > name, it is because the objects are instantiated within another object. So, > it's perfectly alright. > > -- > 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 [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/sympy. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- 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 [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sympy. For more options, visit https://groups.google.com/groups/opt_out.
