This repository has been archived by the owner on Aug 10, 2024. It is now read-only.
V2: Not re-computing when state doesn't change #66
pauleveritt
started this conversation in
General
Replies: 2 comments 2 replies
-
from antidote import state, inject, lazy, world
@state
def counter(previous: int | None) -> int:
return (previous or 0) + 1
@lazy(scope='deterministic')
def greeting(
count: int = inject[counter],
) -> str:
"""Return a greeting."""
return f"({count}): Hello!"
def main() -> None:
"""Process a greeting."""
# return greeting()
# Each time you call it, the count is the same. In fact, `greeting`
# doesn't even get called the second time.
assert world[greeting()] == "(1): Hello!"
assert world[greeting()] == "(1): Hello!"
# But when you update the state, it recomputes.
counter.update()
assert world[greeting()] == "(2): Hello!" |
Beta Was this translation helpful? Give feedback.
2 replies
-
No worries, it happens to everyone. :) You're right it's probably better. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
First: Sorry for the colossally-dumb world[] vs. world.get questions. Also: Moving general V2 questions off that huge PR conversation.
I have a simple example using state:
I had the impression @Inject would not even execute if its dependencies hadn't changed. In the debugger, if I set a breakpoint in
greeting
, it stops 3 times. I only expected twice.Beta Was this translation helpful? Give feedback.
All reactions