brennerm / PyTricks

Collection of less popular features and tricks for the Python programming language
MIT License
3.07k stars 502 forks source link

ifelse comment switch #86

Closed flintforge closed 6 years ago

flintforge commented 6 years ago

Hi, there is more ! This applies to turn either one block as comments OR the next one. In the following example, removing the first # will comment block 1 and decomment block 2


#'''
print('block 1')
'''
print('or block 2 ?')
#'''
brennerm commented 6 years ago

Hey @flintforge, where's the difference to the previous one?

flintforge commented 6 years ago

First example shows how to toggle individual blocks

#'''  < toggling off this # turns the block as a comment (it wasn't, line was mute)
print("you see me")
#'''

second example is the equivalent of C ifdef 0 / ifdef 1 way to comment :

#''' < toggling this # will comment the next block,
print("you saw me now you don't see me")
'''# which will stop here, where it was previously a beginning, hence turning it in an ending block tag
print("you didn't see me, now you do !")
#'''   and so, it uncomments the one below

The rule of thumb is : # mutes a beginning line, but ''' or """ always remains interpreted as an ending for a beginning comment block, be they preceded by a sharp or not.

The example are easy to understand with syntax coloring, but not without. And so the exercise "comment the code about commenting the code inside the code" can get quite complicated! I updated again the file while writing these explanations.