Recently there has been some discussion on RFC-81 about the use of properties and attributes in the stack.
While the consensus seems to be that the use of properties and attributes (instead of getters and setters) is desirable and Pythonic there are some pitfalls when it relates to extension types.
With the upcoming change to pybind11 it is trivial to add properties in the C++ wrapper. However there are a few issues to resolve:
- What to do when the setter is overloaded?
- What to do with immutable vs mutable types?
- What to do with STL container types (that return an equivalent Python container type as a copy, thus silently ignoring modification of an element)?
- What to do when the setter or getter is expensive?
- What do we do with the getter and setter (keep / remove / deprecate)?
This post serves as a platform to decide upon a good course of action.
As an initial proposal I suggest the following:
Add an attribute (or property) when (at least) the following criteria are met:
- The setter and getter accept and return an object of the exact same type (keep setters for conversions).
- There is either a negligible cost, or the value can be cached.
- The returned (set) type is not an STL container type.
Probably supplemented with a rule on mutability vs immutability for which I have no good suggestion other than: “do what NumPy does”.
I invite your comments and suggestions.
See also: