object-literal

Why is the dict literal syntax preferred over the dict constructor?

Why is the dict literal syntax preferred over the dict constructor? Question: Why is the Python dict constructor slower than the using literal syntax? After hot debate with my colleague, I did some comparison and got the following statistics: python2.7 -m timeit “d = dict(x=1, y=2, z=3)” 1000000 loops, best of 3: 0.47 usec per …

Total answers: 1

Are object literals Pythonic?

Are object literals Pythonic? Question: JavaScript has object literals, e.g. var p = { name: “John Smith”, age: 23 } and .NET has anonymous types, e.g. var p = new { Name = “John Smith”, Age = 23}; // C# Something similar can be emulated in Python by (ab)using named arguments: class literal(object): def __init__(self, …

Total answers: 8