Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
disable a block of code
#1
while doing some initial testing, i want to disable a block of code. many other languages have a specific comment start and end symbol. but Python doesn't.

can i achieve this by adding a line with ''' before and after the code to be disabled as long as the indentation resumes at the same depth? i tried and it works. but i don't what pitfalls there are, besides some other existing use of '''.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Yes, Python uses tripe-quotes for block comments as well as strings.
Reply
#3
(Aug-18-2018, 04:10 AM)Skaperen Wrote: while doing some initial testing, i want to disable a block of code.
In a good editor, such as Kate, you can select a block of code and comment it out with a keyboard shortcut.
Reply
#4
i see """ used in lots of places but i never see ''' used. early on i even thought """ was the only allowed way to do triple quoting until i was curious enough to actually try it. so my thought is that i will use ''' since it is less likely to run into a legit string literal using ''' than the others. even so, it would have been nice to have something for block comments like /* in a few other languages. and, IMHO, making it nestable would be a plus (though arguments against that can be easily made).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
When do you actually put that many comments into code?

the triple quotes of docstrings handle descriptions for every piece of code. If you wanted to use it for block comments, you could do that also.

and i always use single quotes for everything in python, unless im trying to avoid escaping one by using double quotes.

Quote:i want to disable a block of code.
besides triple quotes, depending on what i want to switch off and on, I will put my code into a class and just comment out the creation of the instance (assuming that is not referred to again). However thats if it called for being in a class in the first place.
Recommended Tutorials:
Reply
#6
i comment my code as i write it and/or as i rewrite it. but less then 5% is commented. i am trying to increase that percentage, particularly for what i (intend to) release to the public.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python multiple try except block in my code -- can we shorten code mg24 10 6,152 Nov-10-2022, 12:48 PM
Last Post: DeaD_EyE
  Errors when trying to disable tkinter checkbutton rrick_88 7 2,861 Feb-17-2022, 10:30 PM
Last Post: deanhystad
  "If Len(Word) == 0" Code Block in Pig Latin Program new_coder_231013 3 2,066 Jan-02-2022, 06:03 PM
Last Post: deanhystad
  Try,Except,Else to check that user has entered either y or n (Code block pasted) RandomNameGenerator 3 2,337 Jun-29-2021, 08:21 PM
Last Post: RandomNameGenerator
  Block of code, scope of variables and surprising exception arbiel 8 3,427 Apr-06-2020, 07:57 PM
Last Post: arbiel
  zlib decompress error: invalid code lengths set / invalid block type DreamingInsanity 0 6,866 Mar-29-2020, 12:44 PM
Last Post: DreamingInsanity
  an easy way to disable exception handling Skaperen 6 5,469 Jun-02-2019, 10:38 PM
Last Post: Gribouillis
  Use a block of code only one time rlinux57 14 6,218 Sep-21-2018, 12:53 PM
Last Post: rlinux57
  gnureadline: disable temporarily? klaymen 1 2,467 May-08-2018, 11:16 AM
Last Post: Larz60+
  Repeating a block of code owdcoder 8 5,674 Jul-14-2017, 03:37 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020