for / while else doesn't make sense

Steven D'Aprano steve at pearwood.info
Sat May 21 05:56:33 EDT 2016


On Sat, 21 May 2016 02:01 pm, Chris Angelico wrote:

> On Sat, May 21, 2016 at 1:50 PM, Steven D'Aprano <steve at pearwood.info>
> wrote:

>> Would you classify the second line here:
>>
>> print("Hello World!")
>> pass
>>
>>
>> as a bug? What exactly would your bug report be? "pass statement does
>> nothing, as expected. It should do nothing. Please fix."
>>
> 
> Yes, I would. It's not a bug in Python or CPython - it's a bug in the
> second line of code there. It implies something that isn't the case.

What do you think it implies?

What part of the docs for "pass" implies this thing?

help("pass"):

    ``pass`` is a null operation --- when it is executed, nothing 
    happens. It is useful as a placeholder when a statement is 
    required syntactically, but no code needs to be executed, for 
    example:  [examples snipped]



> It's like having this code:
> 
> if True:
>     pass
> elif False:
>     pass
> else:
>     assert True

Hmmm. Well, let see:


py> if True:
...     pass
... elif False:
...     pass
... else:
...     assert True
...
Traceback (most recent call last):
  File "<stdin>", line 6, in <module>
AssertionError


What witchcraft is this?

S
P
O
I
L
E
R
 
S
P
A
C
E

I'm running Python 2, and have redefined True = False.

But even in Python 3, what exactly is the bug supposed to be? The Python 3
compiler, as naive and simple as it is, is perfectly capable of compiling
out the dead code:

py> dis.dis("""if True: pass
... elif False: pass
... else: assert True
... """)
  1           0 LOAD_CONST               0 (None)
              3 RETURN_VALUE


So the worst we can say about this is that it is pointless dead code.


> It's well-defined code. You know exactly what Python should do. But is
> it good code, 

Well, it's not *great* code, that's for sure. Is it good code? That depends.
There are certainly semantic differences between Python 2 and 3, and
without knowing the intention of the author (that would be you) its hard to
say exactly what the code should be. But it's *legal* code that does
exactly what the language documentation says it ought to do. 


> or is it the sort of thing that gets posted here: 
> 
> http://thedailywtf.com/articles/a-spiritual-journey

I'm not sure how that specific Daily-WTF article is relevant.

But in general, is it worthy of Daily-WTF? No, I don't think so. I think
that your "True, False" example could do with some refactoring and cleanup:
perhaps it is dead code that should be completely removed, but that's not
necessarily Daily-WTF worthy. Or it's some part of some sort of Python 2+3
compatibility layer intending to detect rebindings to True or False, in
which case it is certainly ugly (and probably buggy) but may be needed.

As far as my example, using "pass" in the code... no, it's not WTF-worthy
either. It's at worst worth a raised eyebrow. Without knowing the context
of where it came from, it isn't even clear that it is pointless code.
Perhaps it is from a test suite checking that `pass` is correctly parsed,
compiled and executed as a do-nothing statement.



-- 
Steven




More information about the Python-list mailing list