[Tutor] Impossible Else as Exception
Dave Angel
davea at davea.name
Wed Apr 10 13:52:30 CEST 2013
On 04/10/2013 02:18 AM, Jordan wrote:
> Thank you all for the feedback and suggestions. I have never used an
> assertion, before so I will read up on the concept.
One comment about assertion. Even though an optimized run will ignore
the assertion itself, it can save a (small) amount of time avoiding any
other overhead "preparing" for the assertion. So I'd make your if/elif
logic something like this:
if condition == 1:
do something with 1
else: #Note, condition must be 2, unless something above is flawed
assert(condition==2, "Condition must be 1 or 2, something's wrong")
do something with 2
One more comment I didn't notice anybody else point out. Many times,
instead of hoping the code remains consistent, you can make it much more
likely by changing types. For example, instead of naming the conditions
1 and 2, name then True and False. Now, the test becomes:
if condition:
do something with 1
else:
do something with 2
This isn't as perfect an answer as with typed languages, because
somebody can slip in some other type.
--
DaveA
More information about the Tutor
mailing list