Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #63034

Re: Ifs and assignments

Date 2014-01-02 20:16 -0800
From Ethan Furman <ethan@stoneleaf.us>
Subject Re: Ifs and assignments
References <52C59FF6.5000607@allsup.co> <52C5BD90.9020609@islandtraining.com> <mailman.4811.1388704420.18130.python-list@python.org> <roy-8601CA.18361502012014@news.panix.com> <CAPTjJmoA130Q4+2PHLWiPp1FNaB+c6HgUTWpst4Z_qL3qqoR2w@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.4831.1388724082.18130.python-list@python.org> (permalink)

Show all headers | View raw


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~

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: Ifs and assignments John Allsup <pydev@allsup.co> - 2014-01-02 21:44 +0000
  Re: Ifs and assignments Roy Smith <roy@panix.com> - 2014-01-02 18:36 -0500
    Re: Ifs and assignments Chris Angelico <rosuav@gmail.com> - 2014-01-03 11:07 +1100
    Re: Ifs and assignments Chris Angelico <rosuav@gmail.com> - 2014-01-03 11:06 +1100
    Re: Ifs and assignments Ethan Furman <ethan@stoneleaf.us> - 2014-01-02 20:16 -0800
    Re: Ifs and assignments Chris Angelico <rosuav@gmail.com> - 2014-01-03 16:35 +1100

csiph-web