Re: Proposed new conditional operator: "or else"

2014-12-03 Thread Chris Angelico
On Wed, Dec 3, 2014 at 10:59 PM, Tim Chase
 wrote:
> On 2014-12-02 23:05, Dennis Lee Bieber wrote:
>> >   foo == 42 or else
>>
>>   Has a PERL stink to it... like: foo == 42 or die
>
> This statement actually works in Python and I occasionally use it
> when debugging (in the same fashion as one might do printf()
> debugging in C).  It raises a NameError and the program dies with a
> traceback.  Most frequently, it's to prevent the program from
> continuing on to connect to a database and actually make changes.

*gasp* But it's just as fragile as assert! Anyone could just put one
line of code into your program and destroy all your debugging checks!

die = False

Your code has just been made immortal! This is *obviously* a complete
waste of time, since it's that easy to defeat!

> [sorry for the previous message if I failed to cancel the send that
> happened when I accidentally pressed ctrl+enter...typing in the dark]

It was its own joke. :)

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Proposed new conditional operator: "or else"

2014-12-03 Thread Tim Chase
On 2014-12-02 23:05, Dennis Lee Bieber wrote:
> >   foo == 42 or else
> 
>   Has a PERL stink to it... like: foo == 42 or die

This statement actually works in Python and I occasionally use it
when debugging (in the same fashion as one might do printf()
debugging in C).  It raises a NameError and the program dies with a
traceback.  Most frequently, it's to prevent the program from
continuing on to connect to a database and actually make changes.

I've grown up a bit and usually use pdb.set_trace() now, but it's
still in my grab-bag of tools.

-tkc


[sorry for the previous message if I failed to cancel the send that
happened when I accidentally pressed ctrl+enter...typing in the dark]



-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Proposed new conditional operator: "or else"

2014-12-03 Thread Chris Angelico
On Wed, Dec 3, 2014 at 10:56 PM, Tim Chase
 wrote:
> This actually works in Python and I occasionally use  in debugging
> (much like

finish_sentence() or die

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Proposed new conditional operator: "or else"

2014-12-03 Thread Tim Chase
On 2014-12-02 23:05, Dennis Lee Bieber wrote:
> >   foo == 42 or else
> 
>   Has a PERL stink to it... like: foo == 42 or die

This actually works in Python and I occasionally use  in debugging
(much like
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Proposed new conditional operator: "or else"

2014-12-02 Thread Sturla Molden
Dennis Lee Bieber  wrote:

>>   foo == 42 or else
>> 
> 
>   Has a PERL stink to it... like: foo == 42 or die

I think this statement needs to take ellipsis as well

foo == 42 or else ...




Sturls

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Proposed new conditional operator: "or else"

2014-12-02 Thread Michael Torrie
On 12/02/2014 10:18 AM, Roy Smith wrote:
> In the process of refactoring some code, I serendipitously created what I 
> think is an essential new bit of Python syntax.  The “or else” statement.  I 
> ended up with:
> 
> sites_string = args.sites or else self.config['sites']

But isn't that syntactically equivalent of this?

sites_string = args.sites or self.config['sites']

Seems to be what you're looking for with "or else" unless I
misunderstand what you're proposing.

Doing a bit of testing in the interpreter and I find that a combination
of Python's truthiness semantics and short-circuit evaluation seems to
give a consistent result. Consider:

'a word' or False => 'a word'
'a word' or True => 'a word'
False or 'a word' => 'a word'
True or 'a word' => True

Is this similar to what you'd expect with "or else?"


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Proposed new conditional operator: "or else"

2014-12-02 Thread Roy Smith
In article ,
 Chris Angelico  wrote:

> On Wed, Dec 3, 2014 at 4:41 AM, Zachary Ware
>  wrote:
> > On Tue, Dec 2, 2014 at 11:18 AM, Roy Smith  wrote:
> >> Wouldn’t it be neat to write:
> >>
> >>foo == 42 or else
> >>
> >> and have that be an synonym for:
> >>
> >> assert foo == 42
> >>
> >> :-)
> >
> > Never going to happen, but I like it!  Perhaps raise IntimidationError
> > instead of AssertionError when it fails?
> 
> Definitely. That's what I first thought, when I saw the subject line.
> 
> Additionally, whenever this construct is used, the "yield" statement
> (expression, whatever) will be redefined to yield to intimidation and
> make the statement true, whatever it takes. In the above example,
> "yield" would decide which out of foo and 42 is more amenable to
> change, and altering it to be equal to the other. (It may also find
> that == is the most amenable, and alter its definition such that foo
> and 42 become equal.)
> 
> ChrisA

This could be handy in the field of forensic accounting.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Proposed new conditional operator: "or else"

2014-12-02 Thread Sturla Molden
Zachary Ware  wrote:
> On Tue, Dec 2, 2014 at 11:18 AM, Roy Smith  wrote:
>> Wouldn’t it be neat to write:
>> 
>>foo == 42 or else
>> 
>> and have that be an synonym for:
>> 
>> assert foo == 42
>> 
>> :-)
> 
> Never going to happen, but I like it!  Perhaps raise IntimidationError
> instead of AssertionError when it fails?

I guess the "or else" statement should do this:

or else 

where the statement  is executed if the statement 
evaluates to false.

In absence of an explicit threat

   or else

it should raise IntimidationError if  evaluates to false false.

If  evaluates to true it should just return .



Sturla

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Proposed new conditional operator: "or else"

2014-12-02 Thread Andrea D'Amore

On 2014-12-02 17:41:06 +, Zachary Ware said:


foo == 42 or else



Never going to happen, but I like it!  Perhaps raise IntimidationError
instead of AssertionError when it fails?


That should probably be a DONTPANICError in large, friendly terminal 
font letters.


--
Andrea

--
https://mail.python.org/mailman/listinfo/python-list


Re: Proposed new conditional operator: "or else"

2014-12-02 Thread Marko Rauhamaa
Tim Chase :

>> >foo == 42 or else
>
> In light of the parallel thread discussing the "assert" statement and
> the perils of trusting it to be present even though it can be
> optimized away, this "or else" could be (in the altered words of Don
> Corleone), "I'm gonna make an assertion he can't refuse."

I would consider vain threats an antipattern.


Marko
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Proposed new conditional operator: "or else"

2014-12-02 Thread Tim Chase
On 2014-12-02 11:41, Zachary Ware wrote:
> On Tue, Dec 2, 2014 at 11:18 AM, Roy Smith  wrote:
> > Wouldn’t it be neat to write:
> >
> >foo == 42 or else
> >
> > and have that be an synonym for:
> >
> > assert foo == 42
> >
> > :-)
> 
> Never going to happen, but I like it!  Perhaps raise
> IntimidationError instead of AssertionError when it fails?

In light of the parallel thread discussing the "assert" statement
and the perils of trusting it to be present even though it can be
optimized away, this "or else" could be (in the altered words of Don
Corleone), "I'm gonna make an assertion he can't refuse."

(not that I particularly want/expect this in the language definition,
but it's fun to entertain the idea)

-tkc



-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Proposed new conditional operator: "or else"

2014-12-02 Thread Chris Angelico
On Wed, Dec 3, 2014 at 4:41 AM, Zachary Ware
 wrote:
> On Tue, Dec 2, 2014 at 11:18 AM, Roy Smith  wrote:
>> Wouldn’t it be neat to write:
>>
>>foo == 42 or else
>>
>> and have that be an synonym for:
>>
>> assert foo == 42
>>
>> :-)
>
> Never going to happen, but I like it!  Perhaps raise IntimidationError
> instead of AssertionError when it fails?

Definitely. That's what I first thought, when I saw the subject line.

Additionally, whenever this construct is used, the "yield" statement
(expression, whatever) will be redefined to yield to intimidation and
make the statement true, whatever it takes. In the above example,
"yield" would decide which out of foo and 42 is more amenable to
change, and altering it to be equal to the other. (It may also find
that == is the most amenable, and alter its definition such that foo
and 42 become equal.)

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Proposed new conditional operator: "or else"

2014-12-02 Thread Ethan Furman
On 12/02/2014 09:41 AM, Zachary Ware wrote:
> On Tue, Dec 2, 2014 at 11:18 AM, Roy Smith wrote:
>>
>> Wouldn’t it be neat to write:
>>
>>foo == 42 or else
>>
>> and have that be an synonym for:
>>
>> assert foo == 42
> 
> Never going to happen, but I like it!  Perhaps raise IntimidationError
> instead of AssertionError when it fails?

As long as when raising Intimidation, it also roughs up a couple surrounding 
lines as a warning to the rest of the code...

--
~Ethan~



signature.asc
Description: OpenPGP digital signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Proposed new conditional operator: "or else"

2014-12-02 Thread Zachary Ware
On Tue, Dec 2, 2014 at 11:18 AM, Roy Smith  wrote:
> Wouldn’t it be neat to write:
>
>foo == 42 or else
>
> and have that be an synonym for:
>
> assert foo == 42
>
> :-)

Never going to happen, but I like it!  Perhaps raise IntimidationError
instead of AssertionError when it fails?

-- 
Zach
-- 
https://mail.python.org/mailman/listinfo/python-list