【问题标题】:How to list all class cached_properties in Python?如何在 Python 中列出所有类 cached_properties?
【发布时间】:2021-05-31 21:23:13
【问题描述】:

这是一个例子:

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)

列出缓存属性的最简单方法是什么?

【问题讨论】:

    标签: python caching properties


    【解决方案1】:

    我有一个解决方案:

    import inspect
    import functools
    list_of_cached_properties_names = [
        name for name, value in inspect.getmembers(DataSet)
        if isinstance(value, functools.cached_property)
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-03
      • 2010-11-15
      • 1970-01-01
      • 1970-01-01
      • 2018-07-19
      • 1970-01-01
      • 2023-04-07
      相关资源
      最近更新 更多