Skip to content

Instantly share code, notes, and snippets.

@barseghyanartur
Created January 14, 2026 11:58
Show Gist options
  • Select an option

  • Save barseghyanartur/be9a4404bbf3dc5a9edcd1c1cf22d048 to your computer and use it in GitHub Desktop.

Select an option

Save barseghyanartur/be9a4404bbf3dc5a9edcd1c1cf22d048 to your computer and use it in GitHub Desktop.
import functools
import typing
# Use a conditional expression directly.
# This evaluates to the function 'cache' itself, which the @ operator then calls.
@ (functools.cache if not typing.TYPE_CHECKING else lambda f: f)
def a():
print("Aaaa")
a() # Prints "Aaaa"
a() # Returns cached value, prints nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment