The easy answer: Make A a function that either instantiates Aclass (the renamed class) or returns the argument unchanged.
The IMHO unhealthy alternative is to use a metaclass to change the way the constructor works so that there is a magic classmethod called before the constructor, and that magic classmethod can then return a unchanged. Note that this is already implemented in Sage. On Sunday, March 29, 2015 at 6:35:25 PM UTC+2, Martin R wrote: > > Since the above appears to be a very difficult question (:-), I'm going to > add another one - which is actually mentioned in the thread title, but I > somehow forgot to ask... > > Suppose I have a class A with an __init__ method taking a single mandatory > argument: > > class A(SageObject): > > def __init__(self, a, b=None, c=None): > > What I'd like to achieve is that A(a) yields a, if a is an instance of A. > I have read a bit about unique representation and CachedRepresentation, > etc., but what I really want to achieve here is, I hope, much simpler. > > I don't want to use CachedRepresentation, since the arguments of __init__ > would be expensive to cache. > > Many thanks, > > Martin > -- You received this message because you are subscribed to the Google Groups "sage-devel" group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+unsubscr...@googlegroups.com. To post to this group, send email to sage-devel@googlegroups.com. Visit this group at http://groups.google.com/group/sage-devel. For more options, visit https://groups.google.com/d/optout.