Mid-line comment in Python?

25,461

Solution 1

You are correct, the answer is a big fat NO.

Solution 2

Actually if you break your statement into multiple lines you can.

Something like:

['../some/guy', '-m', '10',
# '-p', '0',
 '-n', '100', '-f', '/dev/stdout']

should work.

Share:
25,461
wim
Author by

wim

Hi from Chicago! Python dev with interest in mathematics, music, robotics and computer vision. I hope my Q&A have been helpful for you. If one of my answers has saved your butt today and you would like a way to say thank you, then feel free to buy me a coffee! :-D [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo *Click*

Updated on July 05, 2022

Comments

  • wim
    wim almost 2 years

    I'm wondering if there is any way to comment out part of a line, like you can do in c++ with /*this*/. The only comments I know about are # this which always goes to the end of the line and the """these""" ones, which do not work mid-line.

    Example use-case: using subprocess and need to temporarily comment out an argument -p 0 from the list:

    ['../some/guy', '-m', '10', '-p', '0', '-n', '100', '-f', '/dev/stdout']
    

    It would be nice to have a keyboard shortcut to comment out a selection, at the moment I just copy the whole line as below

    #['../some/guy', '-m', '10', '-p', '0', '-n', '100', '-f', '/dev/stdout']
    ['../some/guy', '-m', '10', '-n', '100', '-f', '/dev/stdout']
    

    I'm expecting a big fat 'no' but I suppose it doesn't hurt to ask, python has surprised me a few times before.

  • Noufal Ibrahim
    Noufal Ibrahim about 13 years
    And to make matters even nicer. If you mark a region of text in Emacs and ask it to comment it out in Python mode, it will automatically break the line into multiple lines and comment out only the section you've marked.
  • ncoghlan
    ncoghlan about 13 years
    It's also not entirely uncommon to see this kind of thing with the commented out section left at the end of the first line.
  • ArtOfWarfare
    ArtOfWarfare almost 11 years
    It's uglier than the method proposed in the the question of duplicating the entire line...
  • aroth
    aroth over 9 years
    Is there a rationale behind the big fat no?
  • WestCoastProjects
    WestCoastProjects almost 7 years
    Before jumping at me a non-pythoner .. first committed stuff on a massive internal data warehouse in 2007 - and built a number of pipelines and machine learning tools in it since then. That said .. this sucks