Google Python Style Guide: Access Control

It should be noted. The syntax is consistent.

If you need to make a difference, it is important that you should use function calls (such as the following naming guidelines) such as get_foo () and set_foo (). If you’re not in the past? It is clear that there is a need to keep in mind.

The first part is clear: for normal access, do not use setters and getters in order to avoid unnecessary function calls. With the addition of functionality, you can use property ().

The second part is not clear. I can not catch the meaning. What does that mean?

    1 answer 1

    The point is that if attribute calculation has a significant cost (CPU, other resources such as network, disk), then you should not use property, which for the user looks like a simple reference to the attribute: obj.attr . In such cases, you should use an explicit call: obj.get_attr() to show that getting attr not free.

    Also, if in the previous version, obj.attr worked, the manual recommends breaking the existing code (and supporting only obj.get_attr() in the new version), so that the user rethink the use of attr in light of the new requirements for its calculation.

    • It turns out: 1) If access to an attribute is trivial, then simply refer to it: obj.attr 2) If attribute calculation requires a minor computational or other cost, then use property and again refer to the attribute as obj.attr . 3) If obtaining an attribute is expensive, then we use the obj.set_attr () and obj.get_attr () explicit methods, which show that receiving / recording the attribute is not free. It turns out so? - pynix
    • Right. In general, if there are no special requirements. - jfs