ID EN
Functools

cached_property

Python 3.11 🇮🇩 Bahasa Indonesia

Ubah metode kelas menjadi properti yang nilainya dihitung satu kali dan kemudian di-cache sebagai atribut normal selama masa pakai instance. Mirip dengan property(), dengan tambahan caching. Berguna untuk properti komputasi mahal dari instance yang tidak dapat diubah secara efektif.

Syntax

PYTHON
@functools.cached_property(func)

Contoh

Example 1
PYTHON
class DataSet:

    def __init__(self, sequence_of_numbers):
        self._data = tuple(sequence_of_numbers)

    @cached_property
    def stdev(self):
        return statistics.stdev(self._data)

See Also

property() cached_property() property() cached_property() property() lru_cache() How do I cache method calls? cached_property()