On Sunday, March 29, 2015 at 9:35:25 AM UTC-7, Martin R wrote:
>
> What I'd like to achieve is that A(a) yields a, if a is an instance of A. 
>
I think that means that A needs to be a factory function rather than a 
normal class:

class Aclass(...):...

def A(a):
    if isinstance(a,Aclass):
        return a
    else:
        return Aclass(a)

 By the time you're hitting `__init__` the object that's being created has 
already been determined: it's the `self` passed to it. And indeed, 
`__init__` doesn't return a meaningful value. It can only modify `self`.

Read up on metaclasses in python to see how you can integrate such 
factory-function behaviour with the class itself, but those are notoriously 
complicated. Using metaclasses is almost certainly the wrong solution 
unless you can prove that any alternative approach is insufficient. 

My initial reaction is that having such tricky behaviour is the wrong 
interface choice, so I'd recommend that you revisit your choice with that 
assumption and then see if you can convince yourself that you really do 
need `A(a) is a` to be True.

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

Reply via email to