What is the difference between 'return dict (result)' and 'return {“result”: result}'? [duplicate]

200
December 02, 2017, at 12:34 PM

This question already has an answer here:

  • Is there a difference between using a dict literal and a dict constructor? 10 answers

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

def index():
    #
    return dict(result=result)
def index():
    #
    return {"result":result}
Answer 1

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.

Answer 2

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.

READ ALSO
Which dataset used in U-Net?

Which dataset used in U-Net?

I am trying to implement U-net and I use https://githubcom/jakeret/tf_unet/tree/master/scripts this link as reference

162
How to split a list-of-strings into sublists-of-strings by a specific string element

How to split a list-of-strings into sublists-of-strings by a specific string element

I have a word list like belowI want to split the list by

120
Python imaplib server prevent disconnecting (imap copy Broken pipe)

Python imaplib server prevent disconnecting (imap copy Broken pipe)

I'm migrating some emails from one host to another and for this purpose I'm using imap copy, a script which takes two imap servers and a mailbox name and copies them from source to destinationI have cloned the repo and wrote a local script to access the class as follows:

187