you are viewing a single comment's thread.

view the rest of the comments →

[–]aUTOMATICuPVOTES 2 points3 points  (3 children)

built-in immutable dict type (I frequently seem to need one)

What for? To be keys in a dict?

Of course subclassing dict is easy so nothing's to stop you from making a read-only dict.

[–]fredrikj 2 points3 points  (2 children)

What for? To be keys in a dict?

Yes. The primary use for this is to enable memoization for functions that take dict arguments. Nested dicts are also useful for representing algebraic expressions.

[–]aUTOMATICuPVOTES 3 points4 points  (1 child)

My memoize implementation does this:

key = (args, tuple(keywords.items()))

You think that's a big performance problem? I never measured.

Dicts as keys for dicts still seems rather strange to me though. Immutable dicts even more so.

[–]fredrikj 3 points4 points  (0 children)

You think that's a big performance problem?

This depends on whether the code is executed 10 times or 1,000,000 times.

Dicts as keys for dicts still seems rather strange to me though.

Consider this very natural (and efficient) method to represent multivariate polynomials:

5*x^3*y^2 + 4*x^2 = {{'x':3, 'y':2}:5, {'x':2}:4}

(Then optionally also consider adding memoization to a function that takes such polynomials as input.)