Skip to content

Commit

Permalink
use functools.partial to enforce instance scope
Browse files Browse the repository at this point in the history
  • Loading branch information
amcclosky committed Jan 30, 2022
1 parent 322663d commit 0de6c0a
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions django_lifecycle/mixins.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from functools import reduce, lru_cache
from functools import partial, reduce, lru_cache
from inspect import isfunction
from typing import Any, List

Expand Down Expand Up @@ -238,9 +238,10 @@ def _run_hooked_methods(self, hook: str, **kwargs) -> List[str]:
# the same hook within the atomic transaction and on_commit
method_name = method_name + "_on_commit"

def _on_commit_func():
method(self)

# Use partial to create a function closure that binds `self`
# to ensure its available to execute later.
_on_commit_func = partial(method, self)
_on_commit_func.__name__ = method_name
transaction.on_commit(_on_commit_func)
else:
method(self)
Expand Down

0 comments on commit 0de6c0a

Please sign in to comment.