Skip to content

Commit

Permalink
Update documentation of call_aside detailing the motivation and benef…
Browse files Browse the repository at this point in the history
…its of the construct.
  • Loading branch information
jaraco committed Sep 29, 2022
1 parent 7e854fe commit 28e42ee
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions jaraco/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,29 @@ def call_aside(f, *args, **kwargs):
"""
Call a function for its side effect after initialization.
The benefit of using the decorator instead of simply invoking a function
after defining it is that it makes explicit the author's intent for the
function to be called immediately. Whereas if one simply calls the
function immediately, it's less obvious if that was intentional or
incidental. It also avoids repeating the name - the two actions, defining
the function and calling it immediately are modeled separately, but linked
by the decorator construct.
The benefit of having a function construct (opposed to just invoking some
behavior inline) is to serve as a scope in which the behavior occurs. It
avoids polluting the global namespace with local variables, provides an
anchor on which to attach documentation (docstring), keeps the behavior
logically separated (instead of conceptually separated or not separated at
all), and provides potential to re-use the behavior for testing or other
purposes.
This function is named as a pithy way to communicate, "call this function
primarily for its side effect", or "while defining this function, also
take it aside and call it". It exists because there's no Python construct
for "define and call" (nor should there be, as decorators serve this need
just fine). "aside" does not mean to suggest "asynchronously"; the
behavior happens immediately and synchronously.
>>> @call_aside
... def func(): print("called")
called
Expand Down

0 comments on commit 28e42ee

Please sign in to comment.