18811, "dlongnecke-cray", "What should the special methods for context managers be named?", "2021-12-07T01:55:15Z"
What should the special methods for context managers be named?
The manage
statement requires a type to define two special methods
in order to be used as a manager:
enterThis()
- Called upon entry, and can return a resource
leaveThis()
- Called upon exit
The current names are a combination of my personal preference
as well as existing convention for special methods (specifically
the This
suffix).
However the names are just placeholders. Below are some options.
For these options, I think we should focus on the base name of the
options rather than any specific decoration (e.g. given the name
foo
, focus on foo
rather than fooThis
or __foo__
).
This is because naming conventions for special methods are still
under discussion, see comments in: What should the name of our standard hashing method be? · Issue #18340 · chapel-lang/chapel · GitHub
enter
/ exit
- These are the names that Python uses
- I scrolled through PEP343 and some earlier drafts of the Python
with
statement but wasn't able to see why these names were
settled on in particular
enter
/ leave
- The names we currently use, I like them because they are symmetrical
- Entirely a personal preference on my part
setup
/ cleanup
- I think these names might be more descriptive w.r.t. what is
happening, I realized that I often found myself describingexit
as the "cleanup" method - The previous pairs of names make me think of critical sections
rather than preparing a context