6

I have used Python occasionally for several months, I know we can use # and """ or ''' to comment. But when i wanted to comment some items of a dictionary, with comment words ('''), i failed.

testItems = {
'TestOne':
{
    "NameId":101
    "Score":99
 },

'''
 'TestTwo':
 {
    "NameId":101
    "Score":99
 }
'''
}

then i get ther error of SyntaxError: invalid syntax pointing to the last ''' position.

I also know there are some indent rule of python language. But i tried so many indent possibility, still fail.

enter image description here

Tingur
  • 162
  • 2
  • 11
  • umm, I think you have a dangling , after your first entry – mortonjt Jun 01 '16 at 05:30
  • 10
    Multiline strings are not comments. – Ilja Everilä Jun 01 '16 at 05:31
  • remove this multiline comment from inside of dictionary. – Hassan Mehmood Jun 01 '16 at 05:32
  • 2
    If you remove `,`, then it is syntactic, but it won't mean what you think. As @IljaEverilä says, `"""` is not a comment but a multiline string; two strings next to each other are syntactically equivalent to a string literal that is their concatenation: `"a" "b" == "ab"`. Thus, `{ 'a': 'b' """ 'c': 'd' """ }` is equivalent to `{'a': "b 'c': 'd' "}`. – Amadan Jun 01 '16 at 05:33
  • @IljaEverilä http://stackoverflow.com/questions/7696924/multiline-comments-in-python – Hassan Mehmood Jun 01 '16 at 05:34
  • 2
    @HassanMehmood: No, they are strings. Any value evaluated as a statement is ignored: `1`, `"foo"`, `""" bar """`. But this is not a statement, it is *inside another literal*, and cannot be ignored. – Amadan Jun 01 '16 at 05:35
  • 1
    @HassanMehmood that does not make it a comment. It is a multiline string expression, but the result is not bound to a name or used in any way. – Ilja Everilä Jun 01 '16 at 05:36
  • 1
    @IljaEverilä, "triple-quotes are a way to insert text that doesn't do anything (I believe you could do this with regular single-quoted strings too), but they aren't comments - the interpreter does actually execute the line (but the line doesn't do anything). That's why the indentation of a triple-quoted 'comment' is important. " from the link you shared, this comment from Demismade more sense than most of the answers – gaganso Jun 01 '16 at 05:38
  • @IljaEverilä yes, you are right. – Hassan Mehmood Jun 01 '16 at 05:42
  • In this scenario problem is in dictionary record entry. dictionary consist key value pair and separated records by comma(,). here in this image he enter 1st record very well but after 1st record it make , for record separation and after , he using multiline string its not a comment. you can't enter a string without any key after using comma (,) because compiler consider there is another record. – Yash Mangla Jun 01 '16 at 05:57
  • thank you guys. I will use # to comment, not the ''', which is a multi line string and can't be ignored like other language comments, – Tingur Jun 01 '16 at 06:38
  • 1
    You're also missing the `,` between the `'NameId'` and `'Score'` key-value pairs in each sub-dictionary. (I suggest adding a comma after _every_ key-value pair, including the last one. That will make changing or rearranging them later much easier... not to mention `diff`ing those changes.) – Kevin J. Chase Jun 01 '16 at 10:56
  • @mortonjt A dangling `,` doesn't hurt in those cases. – glglgl Jun 01 '16 at 11:02

3 Answers3

3

You can only use ''' or """ to comment where strings are allowed as these don't create comments, but just strings.

In the situation you describe, you are not allowed to put a string. Either move the closing } upwards or uncomment your unwanted code part line by line.

Doing

test_items_1 = {
    "NameId":101,
    "Score":99
 }

test_items_2 = {
    "NameId":101,
    "Score":99
}

testItems = {
    'TestOne': test_items_1,
#    'TestTwo': test_items_2,
}

would work as well.

glglgl
  • 89,107
  • 13
  • 149
  • 217
  • There should be a comma between the `"NameId"` and `"Score"` key-value pairs in the first two dictionaries. – Kevin J. Chase Jun 01 '16 at 11:00
  • @KevinJ.Chase Thank you! Just fixed it. – glglgl Jun 01 '16 at 11:01
  • 2
    Come to think of it, "you are not allowed to put a string" isn't right either... Strings are valid dictionary keys. The problem is that it's not followed by a colon and a value. See [Prabhakar's answer](https://stackoverflow.com/questions/37560576/4116239). – Kevin J. Chase Jun 01 '16 at 11:12
2

Values in between ''' or """ within dictionary will be considered as another item, not comment.

In your case the the content between ''' is treated as another item's key in that dictionary.

You have to use # to comment the unwanted codes.

Ex:

testItems = {
'TestOne':
{
    "NameId":101,
    "Score":99
 },

# 'TestTwo':
# {
#    "NameId":101
#    "Score":99
# }

}
Prabhakar
  • 1,138
  • 2
  • 14
  • 30
  • 2
    This is the first answer to explicitly state the problem: The syntax error has nothing to do with what's _inside_ the string, or even the fact that it's a string --- the problem is that the multi-line **dictionary key** `''' 'TestTwo': {...} '''` is not followed by a colon and a value. That's why his IDE drew the red squiggly underline where the `:` should have appeared. – Kevin J. Chase Jun 01 '16 at 11:05
1

As Ilja Everilä mentioned, there is no multi-line comment in python. Although when I copied your code to my pycharm templet, It didn't gave me any error. Still, in your case, I'd recommend you to use single line commenting method (#). Still, '''...''' or """..."""(convert that part to string) could be used but it will just increase your line of code. Coming to your question, you are getting an error because:

Your code could be rewritten as :

testItems = {'TestOne': {"NameId":101, "Score":99} ''' 'TestTwo':{ "NameId":101"Score":99 } ''' }

That's how python reads it, As you want to keep the part in bold, and comment the italics part. You just can't use string. As you can see that python is taking the whole part in braces (Bold + Italics) as single entity. So either use #, or take out that part from there.

You could rewrite as:

testItems = {
    'TestOne':
        {
            "NameId":101,
            "Score":99
        }
    # 'TestTwo':
    #     {
    #         "NameId":101,
    #         "Score":99
    #     }
}