Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.


  • To avoid confusion for the user of the method, the attribute should not be applied on a method that accepts parameters, since the method will always return the same results even when called with different parameter values.
  • This attribute is intended for expensive calculations, it is not recommended for short methods and properties.
  • The modified method will be made thread safe through the use of a lock.


Code Block
doing expensive processing

import Useful.Attributes

class OnceTest:

    def expensiveFunction():
        # Do expensive processing here
        print "doing expensive processing"
        resultOfExpensiveProcessing = 1234

        # ... and return results
        return resultOfExpensiveProcessing

onceTest = OnceTest()
print onceTest.expensiveFunction()
print onceTest.expensiveFunction()