Martijn's answer explains what is in Python, and correctly states that the book is misleading. Since Python programmers as a rule would never sayNone
Assigning a value of
to a variable is one way to reset it to its original, empty state.None
it's hard to explain what Briggs means in a way which makes sense and explains why no one here seems happy with it. One analogy which may help:
In Python, variable names are like stickers put on objects. Every sticker has a unique name written on it, and it can only be on one object at a time, but you could put more than one sticker on the same object, if you wanted to. When you write
F = "fork"
you put the sticker "F" on a string object . If you then write"fork"
F = None
you move the sticker to the object.None
What Briggs is asking you to imagine is that you didn't write the sticker , there was already an "F" sticker on the F, and all you did was move it, from None to None. So when you type "fork", you're "reset[ting] it to its original, empty state", if we decided to treat F = None as meaning None.empty state
I can see what he's getting at, but that's a bad way to look at it. If you start Python and type , you seeprint(F)
>>> print(F)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'F' is not defined
and that means Python doesn't recognize the name NameError, because there is no such sticker. If Briggs were right and F resets F = None to its original state, then it should be there now, and we should seeF
>>> print(F)
None
like we do after we type and put the sticker on F = None.None
So that's all that's going on. In reality, Python comes with some stickers already attached to objects (built-in names), but others you have to write yourself with lines like and F = "fork" and A = 2, and then you can stick them on other objects later (like c17 = 3.14 or F = 10; it's all the same.)F = None
Briggs is pretending that all possible stickers you might want to write were already stuck to the object.None
Don't fear the Exception! Having your program just log and continue is as easy as:
try:
result = simulate(open("myfile"))
except SimulationException as sim_exc:
print "error parsing stream", sim_exc
else:
if result:
print "result pass"
else:
print "result fail"
# execution continues from here, regardless of exception or not
And now you can have a much richer type of notification from the simulate method as to what exactly went wrong, in case you find error/no-error not to be informative enough.