[Tutor] Python GPIO Code Help Needed

Danny Yoo dyoo at hashcollision.org
Mon Oct 27 01:41:12 CET 2014


>> def changed(channel):
>>          newstate = GPIO.input(channel)
>>          change = oldstates[channel] and not newstate
>>          oldstates[channel] = newstate


Hi Bill,

I would suggest documenting the intent of this function, as it isn't
obvious at first.  A comment string would be appropriate.

I would also suggest that the function should be named "isTurnedOn" or
"isTurnedOff", as it is returning True only under a specific
transition: when the old state of the channel is True, and the new
state of the channel is False.  It doesn't check the transition going
the other direction, from False to True.  In contrast, I think of the
word "changed" to mean both directions for the transition.

To use more technical terms: the "XOR" boolean operator is what I was
expecting to see in the definition of changed(), and I was surprised
when didn't see it. :P


More information about the Tutor mailing list