Comparison with False - something I don't understand

Tim Harig usernet at ilthio.net
Sat Dec 4 23:52:29 EST 2010


On 2010-12-05, Tim Harig <usernet at ilthio.net> wrote:
> Another, questionable but useful use, is to ignore the complex accounting
> of your position inside of a complex data structure.  You can continue
> moving through the structure until an exception is raised indicating
> that you have reached a boundary of the structure.

Here is another example in this vein.  A friend was trying to derive a
mathematical formula for determining the possibly distribution of results
from rolling arbitrariy numbers of m n-sided dice and needed several sets
of data in different directions from which to draw conclusions.

I created objects for dice and roles which contained and manipulated
multiple dice.  To generate a listing of all (non-uniq) possible roles,
I would call the first dices increment method read and read the dice
faces into a log until the first dice threw an exception that it could
not be further incremented.  Then I would call reset() on the first dice
and increment the second and so on much like the odometer of a car.

By using exceptions rather then checking the return value of increment,
the state information of the dice was completely isolated to the dice
and did not polute into the role structure; the logic for incrementing
the dice, logging the role state, and rolling over the dice where
all completely seperated and independent of any state; and therefore
reseting multiple previous dice as the higher values on the odometer were
incremented functioned automatically as each dice threw its own exception
recursively rather then requiring logic to handle these multiple rollovers.



More information about the Python-list mailing list