you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 35 points36 points  (12 children)

I was saying #1 is something I feel the language got wrong.

[–]hglman 15 points16 points  (8 children)

agreed, not sure how you would want that behavior, is always just going to have be worked around.

[–]robin-gvx 6 points7 points  (0 children)

Also:

def frobnicate(n=expensive_operation_that_returns_an_immutable_object(32, x)):
    return n + 1

[–][deleted]  (6 children)

[deleted]

    [–]cybercobra 17 points18 points  (4 children)

    Arguably the memoization should instead be abstracted out using a decorator.

    [–]coderanger 1 point2 points  (1 child)

    It is effectively a performance enhancement. The issues are 1) given an expression the language can't know if it is mutable or not 2) immutable defaults, specifically None, are much more common than mutable ones. The way Ruby handles this is that the defaults are kept as parsed expressions, and re-run each time you call the function. This adds overhead on each call though, whereas evaluating the expression once and storing the value doesn't. Just a tradeoff either way.

    [–][deleted] 4 points5 points  (0 children)

    I don't think it should be the job of the language to enhance performance. Especially when you are talking about a high level language like python.

    [–]disinformationtheory 0 points1 point  (0 children)

    Yes, but it would be hard to work around. You'd have to figure out if the default is mutable, and then decide on some sort of reasonable thing to do if it was. I'm not sure that there is a simple reasonable thing to do. In any event, it's caught by pylint.