Is there any use to returning a hash constructed with a dict and not just using curly braces syntax?

In some Python code that I read, I keep noticing this code:

return dict(somekey=somevalue) 

Does this have an advantage:

 return {somekey:somevalue} 

I usually say no, because both objects will belong to the same dict type, but I could be wrong.

+4
python dictionary hash
source share
6 answers
  >>> def foo (): return dict (a = 1)
 ...
 >>> def bar (): return {'a': 1}
 ...
 >>> import dis
 >>> dis.dis (foo)
   1 0 LOAD_GLOBAL 0 (dict)
               3 LOAD_CONST 1 ('a')
               6 LOAD_CONST 2 (1)
               9 CALL_FUNCTION 256
              12 RETURN_VALUE
 >>> dis.dis (bar)
   1 0 BUILD_MAP 1
               3 LOAD_CONST 1 (1)
               6 LOAD_CONST 2 ('a')
               9 STORE_MAP
              10 RETURN_VALUE
 >>> import timeit
 >>> timeit.Timer (foo) .timeit ()
 0.76093816757202148
 >>> timeit.Timer (bar) .timeit ()
 0.31897807121276855

There is no functional difference, but the latter is more effective.

+15
source share

They are semantically identical.

The notation dict( param=value, ... ) restricts your keys to strings that are valid python identifiers.

dict( sequence-of-2-tuples ) actually matches {} .

The designation {} does not impose restrictions on keys. In addition, they are hashed objects.

There are differences in performance.

+5
source share

{somekey:somevalue} can only be used as a literal, it does not allow any loops inside the brackets.

dict takes **kwargs , which means you can, for example, do dict( **locals() ) . dict(somekey=somevalue) is just a good shortcut when you don't want to enter quotation marks and curly braces.

There is a third constructor for dict, dict([(key,val),(key2,val2)]) . Its the most powerful and often used with zip

+2
source share

Python is dynamic, so you can override what dict means in any of your areas, while the Python syntax is completely non-programmable. That way, the Python parser can conclude, when it sees curly braces, that it should build a dict. The expression dict(..) should always be evaluated like any other function call; find name, build tuple argument, etc.

In fact, using container literals such as curly braces {} is the closest you can go to static type declarations in Python.

I think this influenced Python 3's decision to introduce literals such as s = {1,2,3} .


It happens that programmers reassign embedded python! I think this is mostly by mistake and mostly limited to local variables (and the Python namespaces make sure that it can't do too much harm!) Here's the Google Code Search for the reassignment code dict . I think the strangest example is dict = dict() ; by this point, it should be obvious what you are doing!

To do this does not mean that you must do it. Yes, for example, thinking that a dict should really be called hash in python and replacing another built-in hash call is not something you should do:

 hash, checksum = dict, hash 

:-)

Sample code that does this is directly in the Python standard library. That's right, from line 92 shelve.py:

 def __init__(self, dict, protocol=None, writeback=False): self.dict = dict if protocol is None: protocol = 0 self._protocol = protocol self.writeback = writeback self.cache = {} 

This is a very typical example; dict is used as an argument to the method, and no one notices and does not harm, since the method is very short. Use syntax highlighting for inline functions to figure this out, this is my suggestion.

+2
source share

Curly braces are syntactic, so the only advantage is a clearer expression of the dict structure.

+1
source share

dict(somekey=somevalue) exactly matches {'somekey': somevalue} . (Thus, your two examples are not quite clear - in the first case, the key is the string 'somekey' as a key, and in the second it is something like some kind of key.) It is usually better to use dict literals, if only you avoid typing all quotes.

+1
source share

All Articles