Skip to content

Conversation

@JakubBujnowski
Copy link

No description provided.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

Excellent work on this task! Your cache decorator correctly implements all the functional requirements, handling both positional and keyword arguments while maintaining separate caches for each function.

While your solution is fully functional, I want to share a suggestion for improvement. Using a global variable for the cache, as you've done, can sometimes lead to unexpected behavior in larger, more complex applications. A more robust and self-contained approach is to create the cache inside the decorator, allowing each decorated function to have its own private cache captured in a closure. This is a common pattern that enhances reusability and prevents potential side effects.

This is a recommendation for best practice, not a required change. Your solution is great, and I am approving it.


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Footnotes

  1. Rate AI review example

Comment on lines +4 to +7
_function_caches = {}
def cache(func: Callable) -> Callable:
# Write your code here
pass
if func not in _function_caches:
_function_caches[func] = {}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While using a global dictionary works for this task, it's generally better to avoid global state. A more robust and self-contained approach is to create the cache inside the decorator function, allowing each decorated function to have its own cache captured in a closure. This prevents potential issues in larger applications where global variables can be modified unexpectedly.

Consider this alternative structure:

def cache(func: Callable) -> Callable:
    local_cache = {}  # This cache is private to each decorated function

    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        # ... your caching logic here using 'local_cache' ...

    return wrapper

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants