C-like assignment expression?

Paul McGuire ptmcg at austin.rr.com
Sat May 24 09:52:57 EDT 2008


This version is a bit better, since it follows the convention that
'<<' should return self.

class TestValue(object):
    """Class to support assignment and test in single operation"""
    def __init__(self,v=None):
        self.value = v

    """Add support for quasi-assignment syntax using '<<' in place of
'='."""
    def __lshift__(self,other):
        self.value = other
        return self

    def __bool__(self):
        return bool(self.value)
    __nonzero__ = __bool__

-- Paul



More information about the Python-list mailing list