Skip to content

Commit f719307

Browse files
committed
Fix #339: Improve documentation regarding funtion default arguments.
1 parent b32d00b commit f719307

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

docs/index.rst

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,49 @@ often called with the same arguments:
488488
print(e, "-", _get_pep_wrapped.cache_info())
489489

490490

491+
Curiously, default function arguments are not quite handled as one
492+
might expect, and also the use of positional vs. keyword arguments
493+
may lead to surprising results. In the example below, `foo()`,
494+
`foo(1)` and `foo(a=1)` are treated as different function
495+
invocations, with seperately cached results:
496+
497+
.. doctest::
498+
:pyversion: >= 3
499+
500+
>>> @cached(LRUCache(maxsize=100))
501+
... def foo(a=1):
502+
... print(f"foo({a}) called")
503+
...
504+
>>> foo()
505+
foo(1) called
506+
>>> foo()
507+
>>> foo(1)
508+
foo(1) called
509+
>>> foo(1)
510+
>>> foo(a=1)
511+
foo(1) called
512+
>>> foo(a=1)
513+
514+
If consistent behavior is required, a private helper function may
515+
be introduced to avoid ambiguities, e.g.:
516+
517+
.. doctest::
518+
:pyversion: >= 3
519+
520+
>>> def foo(a=1):
521+
... _foo(a)
522+
...
523+
>>> @cached(LRUCache(maxsize=100))
524+
... def _foo(a):
525+
... print(f"_foo({a}) called")
526+
...
527+
>>> foo()
528+
_foo(1) called
529+
>>> foo()
530+
>>> foo(1)
531+
>>> foo(a=1)
532+
533+
491534
.. decorator:: cachedmethod(cache, key=cachetools.keys.methodkey, lock=None, condition=None)
492535

493536
Decorator to wrap a class or instance method with a memoizing

0 commit comments

Comments
 (0)