Search code examples
pythoncoding-style

coding guidelines for python dictionaries


I have a python dictionary, definition of which does not fit in a single line. Could anyone please tell me guidelines for python dictionaries. I currently have this which does not look good to my eyes.

initialstate =  {
                    'state':grid,
                    'f':find_manhattan_distance(grid,goal),
                    'g':0,
                    'h':find_manhattan_distance(grid,goal),
                    'ancestor': None
                }

Solution

  • pep8.py says:

    mydict.py:2:28: E231 missing whitespace after ':'
    mydict.py:1:15: E222 multiple spaces after operator
    

    Try this:

    initialstate = {
        'state': grid,
        'f': find_manhatten_distance(grid, goal),
        'g': 0,
        'h': find_manhatten_distance(grid, goal),
        'ancestor': None
    }
    

    Notice the change in spacing after commas and operators. This version passes all the pep8.py tests.