2
\$\begingroup\$

In this question (What is the best practice between second assignment or dict's value call?), I first ask for the best solution between second assignment or dict's value call.

But, after few days I realize that the real issue in my problem wasn't really what I asked in the title but instead "How to create a property with cached outputs." My question in the title was about a "wrong" way to solve the problem. A way that should not be, because PEP 20 -- The Zen of Python:

There should be one-- and preferably only one --obvious way to do it.

The good answer corresponding of the question body may be this :

Before 3.8 - functools.lru_cache:

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

    @property
    @lru_cache
    def stdev(self):
        return statistics.stdev(self._data)

    @property
    @lru_cache
    def variance(self):
        return statistics.variance(self._data)

Since 3.8 - functools.cached_property:

Transform a method of a class into a property whose value is computed once and then cached as a normal attribute for the life of the instance. Similar to property(), with the addition of caching. Useful for expensive computed properties of instances that are otherwise effectively immutable.

Example:

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)
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

As a rule of thumb, don't change the intent of a question that has answers. Post a follow up.

But, after few days I realize that the real issue in my problem wasn't really what I asked in the title but instead "How to create a property with cached outputs." My question in the title was about a "wrong" way to solve the problem.

If you feel that changing the title will change the meaning of your post then it's best to not do it. This is because other people will come across your post from Google and be left confused why the question is one thing and answers are another.


Adding a comment so it's permanent as "your comment better fit my question on meta then your actual answer, I think"

Honestly, it sounds like you messed up. Which is ok, we all make mistakes. You posted a very specialized question about how to do X. But later you noticed your mistake, you actually wanted to do Y. You're asking to change the question from asking about X to Y. It doesn't matter what X and Y are, because whatever they are it'll make the answers nonsensical. It's not fair on the answerers to be negatively impacted because you made a mistake. Now you just need to own your mistake, post a new question or post an answer.

\$\endgroup\$
7
  • \$\begingroup\$ My question is about changing the title of the question in order to better fit with the intent behind, not changing the meaning with an answer. In this example : from "What is the best practice between second assignment or dict's value call?" to "How to create a property with cached outputs?" \$\endgroup\$ Commented Feb 10, 2020 at 14:23
  • \$\begingroup\$ @DorianTurba That sounds like a completely different question, so I wouldn't change the title here. \$\endgroup\$
    – Mast Mod
    Commented Feb 10, 2020 at 14:28
  • \$\begingroup\$ Then I should change the question's body in order to better fit with the title and be easier to understand ? \$\endgroup\$ Commented Feb 10, 2020 at 14:31
  • 1
    \$\begingroup\$ @DorianTurba Honestly, it sounds like you messed up. Which is ok, we all make mistakes. You posted a very specialized question about how to do X. But later you noticed your mistake, you actually wanted to do Y. You're asking to change the question from asking about X to Y. It doesn't matter what X and Y are, because whatever they are it'll make the answers nonsensical. It's not fair on the answerers to be negatively impacted because you made a mistake. Now you just need to own your mistake, post a new question or post an answer. \$\endgroup\$
    – Peilonrayz Mod
    Commented Feb 10, 2020 at 15:08
  • \$\begingroup\$ I posted another answer but will accept the first one from Sam Mason. Thanks for your help ! @Peilonrayz your comment better fit my question on meta then your actual answer, I think :) \$\endgroup\$ Commented Feb 10, 2020 at 15:38
  • \$\begingroup\$ @DorianTurba Sounds like a good plan. I'm happy I managed to help! I didn't quite grasp the situation when I answered. And your additional comments helped me understand the situation. Also I feel my comment could come across as unfriendly. So I didn't want to post an answer that could be interpreted as an unhelpful and unfriendly mess. :) \$\endgroup\$
    – Peilonrayz Mod
    Commented Feb 10, 2020 at 16:07
  • \$\begingroup\$ I don't think your comment were unfriendly don't worry. It explain well and that's good 👌 \$\endgroup\$ Commented Feb 10, 2020 at 17:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .