4 ms·
That could be accomplished by doing: da_thing = None for oook in blahblah: if oook == something: da_thing = oook break if da_t
by gvx 8y ago
That could be accomplished by doing:
da_thing = None
for oook in blahblah:
if oook == something:
da_thing = oook
break
if da_thing is None:
print ("Not found")
else:
print ('Found', da_thing)
But it does not work if you sometimes want to find `None`, so better would be something like:
da_thing = sentinel = object()
for oook in blahblah:
if oook == something:
da_thing = oook
break
if da_thing is sentinel:
print ("Not found")
else:
print ('Found', da_thing)
- zbentley 8y agoThis is a common problem in languages which allow the use of "not set" or "not found" indicators as an rvalue, which (sadly) is most of them. Put another way, Python's 'None' or Perl's 'undef' conflate the notion of 'something which has never been set' with the bottom (null) value, aka. the notion of 'the thing here is empty or not useful/usable'. That's a sad conflation to make because it confuses temporal/procedural effects (what has previously been done to this variable?) with contents (does this variable contain a usable value?) JavaScript takes this to an even more ludicrous place by having both 'null' and 'undefined', putting them close to each other in the truth table, and allowing them both as rvalues. Does anyone know of any procedural programming languages (or at least languages that encourage mutation, initialization-without-assignment, and rebinding) that have a) a special value for 'never accessed' and b) do not allow that value to be set, only read?
- gvx 8y agoPython's None is exclusively for 'the thing here explicitly doesn't exist' ('the thing here is empty' would be an empty collection and 'something which has never been set' is signified by raising an exception). I think that's the closest you're gonna get to the language you asked for in your last paragraph, because not being able to delete or unset a variable seems to be a serious design flaw to me.