Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python comments Fail using """ or ''' in dictionary [duplicate]

Tags:

python

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

like image 882
Tingur Avatar asked Jun 01 '16 05:06

Tingur


2 Answers

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.

like image 89
glglgl Avatar answered Nov 19 '22 05:11

glglgl


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
# }

}
like image 34
Prabhakar Avatar answered Nov 19 '22 05:11

Prabhakar