views:

268

answers:

4

Why would one use one over the other:

  • d = dict() ~0.208 usec
  • d = {} ~0.06 usec
+6  A: 

d=dict() requires a lookup in globals(), d={} doesn't

gnibbler
Nope, `dict` is in `__builtin__`.
Mike Graham
+3  A: 

If people use (just) dict() over (just) {}, it's generally because they don't know about {} (which is quite a feat), or because they think it's clearer (which is subjective, but uncommon.)

There are things you can do with dict that you can't do with {}, though, such as pass it to something that expects a callable, like collections.defaultdict(dict). There's also the fact that you can call dict with keyword arguments, which some people prefer:

>>> dict(spam=1, ham=2)
{'ham': 2, 'spam': 1}

Personally, I prefer the dict literal syntax because it works better when you want to use keys that are not valid identifiers:

>>> dict(pass=1)
 File "<stdin>", line 1
    dict(pass=1)
        ^
SyntaxError: invalid syntax
>>> dict('ham and eggs'=1)
  File "<stdin>", line 1
SyntaxError: keyword can't be an expression

(and mixing styles just because some keys are not valid identifiers, yuck.)

Thomas Wouters
+7  A: 

I'm one of those who prefers words to punctuation -- it's one of the reasons I've picked Python over Perl, for example. "Life is better without braces" (an old Python motto which went on a T-shirt with a cartoon of a smiling teenager;-), after all (originally intended to refer to braces vs indentation for grouping, of course, but, hey, braces are braces!-).

"Paying" some nanoseconds (for the purpose of using a clear, readable short word instead of braces, brackets and whatnots) is generally affordable (it's mostly the cost of lookups into the built-ins' namespace, a price you pay every time you use a built-in type or function, and you can mildly optimize it back by hoisting some lookups out of loops).

So, I'm generally the one who likes to write dict() for {}, list(L) in lieu of L[:] as well as list() for [], tuple() for (), and so on -- just a general style preference for pronounceable code. When I work on an existing codebase that uses a different style, or when my teammates in a new project have strong preferences the other way, I can accept that, of course (not without attempting a little evangelizing in the case of the teammates, though;-).

Alex Martelli
"hoisting some lookups out of loops" - what this mean?
Tshepang
@Tshepang, e.g. instead of `for i in x: f(dict())` (which does `len(x)` lookups for name `dict`), first bind a local `d=dict` outside the loop, then `for i in x: f(d())` which does a faster _local_ lookup for name `d`). It's a fundamental Python technique to optimize some loops when they're proven (by profiling, of course) to be performance bottlenecks.
Alex Martelli
Incidentally (and not related to this question at all, of course), Unladen Swallow should make this kind of idiom (d=dict) unnecessary :)
rbp
@Alex, what about `s=str()` vs. `s=''`?
Tshepang
+1  A: 

Like Thomas said, I use dict() so I can specify keywords. Especially if I'm manually constructing a large dictionary for data initialization or whatnot: being able to use keyword syntax saves me two keystrokes (and the associated visual clutter) for every element.

Vicki Laidler