These last three weeks have been really stressful for me due to various reasons. Anyway, I managed to get some spare time to do something funny, like the self-replicating script posted in the previous entry.
Sometimes, when we're working on a problem, we need to denote a variable with an unknown value. C and SQL uses
for this purpose, while Lisp uses
nil and Python have
None. Let's look python closer: sometimes,
means none. But there are some cases we need another semantic interpretation. For example, I like to work with
the unknown value, which means something different to none (
None) which I use for different a purpose.
We can do this:
class Unknown: pass [...] if b is Unknown: print("Var b is unknown")
This works because in python, Classes are also objects. The is operator only returns True if two objects are the same instance. Even better, the is operator allows an interesting syntax when used with not:
if b is not Unknown: [...]
This not only enhances (IMHO) code legibility, it also make comments useless: the code is the comment itself.
Finally, this idea can be also used to define your own Infinitum value:
class Inf(object): pass try: b = 1 / 0 except ZeroDivideError: b = Inf if b is Inf: print "b is Inf"