c james <[EMAIL PROTECTED]> wrote:

> Thanks, I was trying to eliminate another level of indirection with a
> test at each invocation of __call__
> 
> 
Try using different subclasses for each variant:

class YesNo(object):
    def __new__(cls, which, *args, **kw):
        if cls is YesNo:
            if which:
                return object.__new__(Yes)
            else:
                return object.__new__(No)

    def __init__(self, which, *args, **kw):
        print "New", self.__class__, which, args, kw

    def __call__(self, val):
        raise NotImplementedError()


class Yes(YesNo):
    def __call__(self, val):
        print 'Yes', val


class No(YesNo):
    def __call__(self, val):
        print 'No', val

                
>>> y = YesNo(True)
New <class '__main__.Yes'> True () {}
>>> y('hello')
Yes hello
>>> n = YesNo(False)
New <class '__main__.No'> False () {}
>>> n('hello')
No hello
>>> 
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to