Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a way to check if a property has already been cached #32

Open
Tinche opened this issue May 13, 2016 · 2 comments
Open

Add a way to check if a property has already been cached #32

Tinche opened this issue May 13, 2016 · 2 comments

Comments

@Tinche
Copy link
Contributor

Tinche commented May 13, 2016

Sometimes it's useful to be able to check if a property has been cached already. Currently I'm using something like:

    @cached_property
    def cachable(self): pass

    def a_function(self):
        if 'cachable' in self.__dict__:
            # Already cached.
        else:
            # Not cached yet.

(Haven't tested this, but I think it'ill work) Can we think of a better API?

@pydanny
Copy link
Owner

pydanny commented May 13, 2016

Works for me. I like how it plugs right into the very obvious way of accessing things in the object
s __dict__ attribute.

👍

@Tinche
Copy link
Contributor Author

Tinche commented May 14, 2016

It works, I just consider it ugly and it exposes an implementation detail. I'm not really a fan of invalidation either (del self.__dict__['cachable']).

cached_property.iscached(self.cachable)? self.cachable.iscached has some issues. (What if the cachable has a different iscached attribute? We could do what namedtuple does and call it _iscached, not because it's protected but to avoid clashes.) Could we do the same for invalidate()?

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

No branches or pull requests

2 participants