4 ms·
I've used the loop -> else construct sparingly - there are times when it is really convenient. But it is VERY confusing. Its very confusing, and poor design -
by SmirkingRevenge 8y ago
I've used the loop -> else construct sparingly - there are times when it is really convenient.
But it is VERY confusing.
Its very confusing, and poor design - IMHO - because the presence of an "else" block, in the typical case, generally indicates that the preceding block was never executed.
When it comes to while/for blocks in python, the keyword literally indicates the opposite - that the preceding block executed without hiccups - and so also execute this block.
Its a great feature - but there should have been another keyword for it: "also" (or something like that)
- wwwtyro 8y agoHold up. Stop. Read the parent closely. This is a case where the right answer is only obvious once it's been stated. > Its a great feature - but there should have been another keyword for it: "also" SmirkingRevenge - !@#$ing brilliant. You've stepped outside the box and presented an answer that will click as soon as it's read. Someone make a PEP.
- hueving 8y ago"nobreak" also makes as much sense. Even Raymond Hettinger has suggested that, but getting a new keyword at this point in python is going to be really difficult.
- SmirkingRevenge 8y ago"nobreak" also works, and arguably has less ambiguity than my suggestion. And I agree getting a new keyword for such a limited use-case feature would be difficult and doesn't feel likely.
- gvx 8y agoHonestly, "else" wasn't confusing to me at all, but "also" would make me very confused.
- SmirkingRevenge 8y agoThat may be the nicest reply I've ever had on HN. Thanks!
- Gibbon1 8y agoNot a python programmer (I've used it off and on in a very slashy sort of way). I'd much prefer syntax like da_thing = for ook in blahblah: if oook == something: break oook if da_thing is None: print ("Not found") else: print ('Found', da_thing)
- gvx 8y agoThat 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.