evaluates expr once in each Wolfram Language session, always returning the result from the first evaluation.


evaluates expr once and caches the result in persistence location loc.

Details and Options

  • When first used in a session, Once[expr] evaluates expr and returns the result.
  • Subsequent uses of Once[expr] do not evaluate expr again, and return the result from when expr was first evaluated.
  • Once has attribute HoldFirst.
  • Once[expr] is equivalent to Once[expr,"KernelSession"].
  • A persistence location is given as PersistenceLocation["type"] or "type".
  • Unset[Once[expr]] or Unset[Once[expr,loc]] removes any stored value for expr.
  • Once has the following options:
  • ExpirationDateNonewhen the cache should expire
    PersistenceTimeNonehow long the cache should persist
  • If Once[expr,] is evaluated after a cached result is specified to have expired, then expr will be evaluated again.


open allclose all

Basic Examples  (1)

Evaluate an expression inside Once:

Click for copyable input

Any subsequent evaluation of Once[expr] simply returns the previous result:

Click for copyable input

Scope  (2)

Options  (2)

Applications  (2)

Properties & Relations  (3)

Possible Issues  (2)

See Also

LocalCache  Needs  PersistenceLocation  ScheduledTask

Introduced in 2016
| Updated in 2017