cached-property icon indicating copy to clipboard operation
cached-property copied to clipboard

cached_property decorator doesn't work with mangled method names

Open graingert opened this issue 6 years ago • 2 comments

See also https://code.djangoproject.com/ticket/29478#ticket

import itertools

from cached_property import cached_property

count = itertools.count()
count2 = itertools.count()
count3 = itertools.count()


class Foo:
    @cached_property
    def __foo(self):
        return next(count)

    @cached_property
    def foo2(self):
        return next(count2)

    @property
    def foo3(self):
        return next(count3)

    def run(self):
        print('foo', self.__foo)
        print('foo', self.__foo)
        print('foo', self.__foo)
        print('foo', self.__foo)
        print('foo2', self.foo2)
        print('foo2', self.foo2)
        print('foo2', self.foo2)
        print('foo2', self.foo2)
        print('foo2', self.foo2)
        print('foo3', self.foo3)
        print('foo3', self.foo3)
        print('foo3', self.foo3)
        print('foo3', self.foo3)
        print('foo3', self.foo3)


Foo().run()


"""
python cached_property_test.py 
foo 0
foo 1
foo 2
foo 3
foo2 0
foo2 0
foo2 0
foo2 0
foo2 0
foo3 0
foo3 1
foo3 2
foo3 3
foo3 4
"""

graingert avatar Jun 07 '18 11:06 graingert

Here's a django PR that fixes it: https://github.com/django/django/pull/10033

graingert avatar Jun 08 '18 00:06 graingert

@graingert, would you or anyone else like to open a pull request to support this here?

pydanny avatar Sep 30 '18 16:09 pydanny