Re: Python has Traps; Does Euphoria have Programming Traps?

new topic     » goto parent     » topic index » view thread      » older message » newer message
_tom said...

The following lines execute without any warning or error messages:

? 0 <= 5 <= 9 
    --1 
? 0 <= -5 <= 9 
    --1 

Python (and a few other languages) allow this:

Python 2.7.3 (default, Sep 26 2012, 21:53:58)  
[GCC 4.7.2] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> 0 <= 5 <= 9 
True 
>>> 0 <= -5 <= 9 
False 
>>>  
 

The Euphoria way of writing:

? 0 <= 5 and 5 <=9 
    -- 1 
? 0 <= -5 and 5 <= 9 
    -- 0 

How do I explain what is happening?

The explanation is simply that Python supports chained conditionals whereas Euphoria doesn't. This would only be a trap for people coming from Python to Euphoria, as most languages do not support chained conditionals. C/C++ and Java, for example, do not.

The syntax ... ExprA CondA ExprB CondB ExprC is evaluated differently in the two languages.

In Python it is equivalent to ...

TempA = ExprA 
TempB = ExprB 
if TempA CondA TempB is TRUE then 
   TempC = ExprC 
   return TempB CondB TempC 
else 
   return FALSE 
end if 

And in Euphoria it is equivalent to ...

( ( ExprA CondA ExprB ) CondB ExprC ) 

Note that in both languages the expressions are only evaluated once, which is important if they have side effects. However, in Python, ExprC only gets evaluated if the first conditional test is true whereas in Euphoria it always gets evaluated.

For example ...

   0 <= func(X) <= 9 

To get the same effect in Euphoria as in Python, one cannot simply recode it as ...

  ( 0 <= func(X) ) and ( func(X) <= 9 ) 

because then the function is executed twice. Instead one needs to do ...

   object tempA 
   tempA = func(X) 
   (0 <= tempA) and (tempA <= 9) 
new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu