Ifs and assignments
Ethan Furman
ethan at stoneleaf.us
Thu Jan 2 23:16:51 EST 2014
On 01/02/2014 04:06 PM, Chris Angelico wrote:
>
> Here's a crazy idea. Suppose we have a "sticky falseness" that can
> quietly propagate through an expression the way a NaN can... then we
> could just float that right through the .group() call.
>
> class truth:
> def __new__(cls, x):
> if x: return x
> return object.__new__(cls)
> def __bool__(self):
> return False
> def __getattr__(self, name):
> return self
> def __call__(self, *args, **kwargs):
> return self
> def __repr__(self):
> return repr(False)
>
> Pass any object through truth() and it'll either stay the same (if
> it's true) or become this object (if it's false). You can then carry
> on with other method calls, and they'll all happily return false.
An interesting idea. You'd need to add (at least) __getitem__, and I'll
probably call it `maybe`, myself. ;)
> (I'm not sure if I'm using __new__ correctly; I've never actually done
> it in production code, and the info I found online was mainly Py2
> examples. Should that be done with super(), or is that applicable only
> once there's an actual instance with a real MRO?)
I haven't tested it, but your __new__ looks fine. The only thing you
lose by not calling super() is the inability for cooperative multiple
inheritance, except as the ultimate base class.
--
~Ethan~
More information about the Python-list
mailing list