2

How to write a snippet for 'main' function in vs code? It's not allowing to put double "" in between the body. Here is what I have written:

"Print to console": {
        "prefix": "mai",
        "body": [
            "if __name__ == __main__:"
            
        ],
        "description": "This throws main"

And I want this:

if __name__ == '__main__':
   pass

How to make this snippet?

0

2 Answers 2

2

Try this one.

"Print to console": {
        "prefix": "mai",
        "body": [
            "if __name__ == '__main__':",
            "\tpass"
        ],
        "description": "Whatever you want to write."
    }

Sign up to request clarification or add additional context in comments.

Comments

1

Paste into https://snippet-generator.app/ that formats it into the necessary json. You copy that (there’s a button) and then you just paste it into your snippet file.

For who need to know I'd guess you just have to do the right escape "foo" : "\"bar\"" (I think). Then again, Python is OK with single quotes and json doesn't care about about those so "foo" : "'bar'" would also bear looking into. Snippet generator does not round trip, so I do typically have to edit existing snippets with the correct escape/quotes strategy, but it's easier to get 95% of the thing right and then do tweaks than write embedded Python in json all the time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.