Hacker News new | ask | show | jobs
by kazinator 2244 days ago
Under the sane semantics implemented in every major language other than Python, if you want to capture a snapshot of something at definition time, you can put it into a variable:

  wildly_changing_variable = random_initializer();

  stable_snapshot = wildly_changing_variable

  def foo(arg = stable_snapshot):
    ...
Don't touch stable_snapshot and everything is cool. This is the rare case. Of course

  def foo(arg = wildly_changing_variable)
means we want the current value.

I don't understand your "closure" comments; either way, things are being lexically closed. It's a question of when evaluation takes place, not in what scope or under what scoping discipline.

In fact, Python's treatment requires the implementation to have a hidden storage that is closed over; the equivalent of my stable_snapshot variable has to be maintained by the implementation. The definition-time evaluation has to stash the value somewhere, so to that it can use that one and not the current.