Python Forum

Full Version: Help unwinding assignment
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to understand the operation of this assignment function:

 Iss = (0.0 < tNow < 2.0) * Iin + (tNow >= 2.0 or tNow < 0) * 0 


Can anyone help me understand this:
Not surprised that you don't understand it since it doesn't make any sense.

This is True if tNow > 0 and tNow < 2, else it is False
(0.0 < tNow < 2.0)
That result is multipled by Iin which is a float. When multiplying a boolean by a float, True = 1 and False = 0, so this is either Iin or 0.0 depending on the value of tNow.
(0.0 < tNow < 2.0) * Iin
Now for the makes no sense part. 0 * 0 == 0 and 1 * 0 == 0, so this is always zero.
(tNow >= 2.0 or tNow < 0) * 0
Which means these are equivalent:
Iss = (0.0 < tNow < 2.0) * Iin + (tNow >= 2.0 or tNow < 0) * 0
Iss = (0.0 < tNow < 2.0) * Iin
Which I think is easier to understand as:
Iss = Iin if (0.0 < tNow < 2.0) else 0
Thanks for the help. I cut the Python code into the application, and it still worked great.
I'm attempting to translate a Python-based neuron simulator to Delphi, my development language of choice, so the Delphi result might be interesting to you:
if ((0 < tNow) AND (tNow < 2)) then
      Iss := Iin
    else
      Iss := 0;
Really quite simple when understood correctly.

... Jim