-2

Is there any difference in the following processing before passing it to template?

def index():
    #
    return dict(result=result)

def index():
    #
    return {"result":result}
re3
  • 11
  • 6

2 Answers2

0

As you can see, the only difference is in the syntax. Both return a new, regular, ordinary dictionary object, with one key/item pair. But be aware that the second form requires the key to be in quotes, whereas the first form does not. Depending on your situation, one might be much better than the other. Personally, I prefer the first form, although the second is more flexible.

GaryMBloom
  • 5,350
  • 1
  • 24
  • 32
0

The only noticeable difference is that dict() technically is a global.

In [1]: def index():
   ...:     result = "abc"
   ...:     return dict(result=result)
   ...: 

In [2]: index()
Out[2]: {'result': 'abc'}

In [3]: def dict(result):
   ...:     return "def"
   ...: 

In [4]: index()
Out[4]: 'def'

This is not normally worth noting.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415