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

No compatible with __slots__ #69

Open
DevOpsCraftsman opened this issue Aug 30, 2017 · 3 comments
Open

No compatible with __slots__ #69

DevOpsCraftsman opened this issue Aug 30, 2017 · 3 comments

Comments

@DevOpsCraftsman
Copy link

DevOpsCraftsman commented Aug 30, 2017

Hi. Very interresting project.
I was about to develop the same thing, but made a research first and found this

The problem is that it doesn't support the use of __slots__

Demonstration:

>>> from cached_property import cached_property
>>> class C:
...     __slots__ = 'x',
...     @cached_property
...     def y(self):
...         return 'y'
... 
>>> c = C()
>>> c.y
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.6/site-packages/cached_property.py", line 26, in __get__
    value = obj.__dict__[self.func.__name__] = self.func(obj)
AttributeError: 'C' object has no attribute '__dict__'

I have a little wrapper for this named slot_wrapper somewhere.

Here is a simplified version:

def slot_wrapper(attrname, slot, cls):

    class InnerDescriptor:

        def __get__(self, obj, cls):
            return slot.__get__(obj, cls)

        def __set__(self, obj, val):
            slot.__set__(obj, val)

return InnerDescriptor(attrname)

It takes the descriptor under the slot and replace it (__slots__ underly special descriptors, but they remains descriptors after all).
It needs adjustments with your code in order to work... I use it with metclass so it got instaciate after the creation of a class wich have __slots__.

My demand is between a bug report, a feature request and an enhacement proposal!

@Nath-P
Copy link

Nath-P commented Oct 2, 2019

a workaround for this is to add "__dict__" to the the items slots. (got the idea to do this from https://docs.python.org/3/reference/datamodel.html#slots)

@althonos
Copy link

althonos commented Oct 2, 2019

Or use property-cached 😉

@ZhymabekRoman
Copy link

Bump, same problem

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

5 participants