Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list all class cached_properties in Python?

Here is an example:

import statistics
from functools import cached_property


class DataSet:
    def __init__(self, sequence_of_numbers):
        self._data = sequence_of_numbers

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

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

What is the easiest way to list cached properties?

like image 378
Aleksandr Gavrilov Avatar asked Sep 11 '25 16:09

Aleksandr Gavrilov


1 Answers

I have a solution:

import inspect
import functools
list_of_cached_properties_names = [
    name for name, value in inspect.getmembers(DataSet)
    if isinstance(value, functools.cached_property)
]
like image 140
Benoit Avatar answered Sep 14 '25 06:09

Benoit