[Tutor] use of __new__

spir denis.spir at free.fr
Fri Jun 12 13:54:47 CEST 2009


Hello,

I have (again) some issue using __new__.
What I need is basically to catch an object creation and yield an object of an alternate type, when a condition is met.

Basically, the outline looks like:

class Normal(object):
    def __new__(cls, arg):
        if cond(arg):
            # <yield instance of Special>
            # expression is simply: Special(arg)
            # must be __init__ialised !
        
        # Conceptually, nothing to change:
        # <yield instance of Normal>
        # must be __init__ialised !

But I got issues in both cases:
* cannot find how to get Special objects initialised
* cannot find how to return Normal object
(also Normal's supertype has no explicite __new__, but it has an __init__)

Denis
------
la vita e estrany


More information about the Tutor mailing list