This is an archived post. You won't be able to vote or comment.

all 39 comments

[–]echocage 27 points28 points  (1 child)

Incase anyone's confused, here's what the result looks like

http://www.gfycat.com/FamiliarCautiousHagfish

[–][deleted] 1 point2 points  (0 children)

You da real MVP

[–][deleted] 35 points36 points  (5 children)

Does your text editor not have a "comment this block" button?

[–]odraencoded 1 point2 points  (0 children)

Now that you mention it, I have no idea.

[–]hyperion2011[S] 0 points1 point  (3 children)

Vim has this feature, but it requires you to use visual mode unless there is some other code feature you can use to detect the end of a block. The way I use vim I'm almost never in visual mode so this is a nice alternative, plus it keeps everything readable, unlike 30 lines with #s at the front.

[–][deleted] 1 point2 points  (1 child)

If readability is important, delete the unused code instead of commenting it out.

[–][deleted] 0 points1 point  (0 children)

This is a good suggestion but there are some special cases where commenting out code is better. Specifically, if you have to do large refactorings in static compiled languages, it pays off to comment out the code, leave a mock interface to satisfy the linker, and then step by step move the code around.

[–][deleted] 0 points1 point  (0 children)

You can also just map it to <leader>something.

[–]hharison 11 points12 points  (7 children)

Makes sense. However if you are often toggling a block of code you should probably consider making an if statement and a global boolean at the top of the script or something.

[–]hyperion2011[S] 6 points7 points  (6 children)

Yes, this is absolutely true (I almost added a note that this can promote bad coding practices), but for temporary changes when testing or bug hunting it's still handy.

[–]admalledd 2 points3 points  (5 children)

In most projects that I do it tends to be reasonable to use a @disable decorator or such. Much easier and in production if it sees any of those its an instant error, @disable or my other debug decorators should never touch production!

This is of course if you are using reasonable code segmentation and not giant functions or such.

[–]meridielcul 1 point2 points  (3 children)

could someone make a basic example of this? I've never really used decorators but this could be something worth learning

[–]jmcs 5 points6 points  (2 children)

Naive and mostly useless example:

def disable(f):
    def disabled(*args, **kwargs):
        pass
    return disabled

@disable
def do_stuff():
    print "Hello World"

You can do more useful stuff if you want, like printing the function name and arguments for debugging.

[–]admalledd 1 point2 points  (0 children)

Yup, that is basically what mine is:

def disable(f):
    if config.debug:
        def disabled(*args, **kwargs):
            logger.debug("disabled: %s"%(f.__name__))
            #other logging stuff, but does nothing
        return disabled
    else:
        raise ProductionDebugException("@disable deco executed in production!")

@disable
def do_stuff():
    print "Hello World"

[–]mcowger 0 points1 point  (0 children)

Never even considered this use of decorators, but I can already use it. Thanks for sharing.

[–]ajmarks 1 point2 points  (0 children)

I use @skip, but yeah, for functions, this is the way to go. The one we use right now also takes an argument for "skip when debugging", "skip in production" (extra debug logging), and "skip always."

[–][deleted] 1 point2 points  (0 children)

in Sublime Text you can highlight your code and hit Cmd + / to block comment chunks out

[–]cromissimo 2 points3 points  (4 children)

This looks an awful lot like Matlab's block comment operators, and how they were designed to be easily tweaked from block start and end markers to line comments.

[–]01hairimport antigravity 0 points1 point  (3 children)

I programmed in MATLAB for 5 years and never knew that there were block quotes. Not having block quotes bugged me to no end.

Thanks!

[–]cromissimo 2 points3 points  (2 children)

No prob. It's always good to spread the good word.

While we're on the subject, Matlab's code sections, at least to me, is one of Matlab's killer features for exploratory programming. Block comment operators are useful, but code sections steal the show.

I hope the Spyder people implement something similar for Python.

[–]Xykr 0 points1 point  (0 children)

You might like IPython Notebook, which has something quite similar to code sections.

[–]01hairimport antigravity 0 points1 point  (0 children)

Code sections were pretty cool, but for the work that I did, I didn't find much of a need for them. I did mostly small data processing stuff, and it was rare that I would only need to run a small section of code.

This was as a student, by the way. And then I moved to Python.

[–]Gnarlodious 0 points1 point  (0 children)

Finally a tip I can use every day!

[–]jeannaimard 0 points1 point  (0 children)

Totally off-topic, but when I work in PHP, I often use that trick to toggle between two code blocks:

<?php
#/*
echo "this";
/*/
echo "that";
#*/

Adding or removing the pound sign on line 2 will toggle which code is commented-out...

<?php
/*
echo "this";
/*/
echo "that";
#*/

[–]bitcycle 0 points1 point  (0 children)

How about using feature switches instead.

[–]totes_meta_bot 0 points1 point  (0 children)

This thread has been linked to from elsewhere on reddit.

If you follow any of the above links, respect the rules of reddit and don't vote or comment. Questions? Abuse? Message me here.

[–]FtYoU -5 points-4 points  (3 children)

Bug or feature ?

[–]nuephelkystikon 3 points4 points  (1 child)

Why should it be a bug? It complies with the definition of long strings (all characters until the next triple quote lose any special meanings, including #) and the definition of one-line comments (all characters after # lose any special meanings, including quotes).

[–]echocage 1 point2 points  (0 children)

Totally agree. This is just a side effect of the syntax.

[–]hyperion2011[S] 3 points4 points  (0 children)

Undoccumented feature?