Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 916535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:04:07+00:00 2026-05-15T18:04:07+00:00

There’s a little thing I want to do in Python, similar to the built-in

  • 0

There’s a little thing I want to do in Python, similar to the built-in property, that I’m not sure how to do.

I call this class LazilyEvaluatedConstantProperty. It is intended for properties that should be calculated only once and do not change, but they should be created lazily rather than on object creation, for performance.

Here’s the usage:

class MyObject(object):

    # ... Regular definitions here

    def _get_personality(self):
        # Time consuming process that creates a personality for this object.
        print('Calculating personality...')
        time.sleep(5)
        return 'Nice person'

    personality = LazilyEvaluatedConstantProperty(_get_personality)

You can see that the usage is similar to property, except there’s only a getter, and no setter or deleter.

The intention is that on the first access to my_object.personality, the _get_personality method will be called, and then the result will be cached and _get_personality will never be called again for this object.

What is my problem with implementing this? I want to do something a bit tricky to improve performance: I want that after the first access and _get_personality call, personality will become a data attribute of the object, so lookup will be faster on subsequent calls. But I don’t know how it’s possible since I don’t have a reference to the object.

Does anyone have an idea?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:04:08+00:00Added an answer on May 15, 2026 at 6:04 pm

    I implemented it:

    class CachedProperty(object):
        '''
        A property that is calculated (a) lazily and (b) only once for an object.
    
        Usage:
    
            class MyObject(object):
    
                # ... Regular definitions here
    
                def _get_personality(self):
                    print('Calculating personality...')
                    time.sleep(5) # Time consuming process that creates personality
                    return 'Nice person'
    
                personality = CachedProperty(_get_personality)
    
        '''
        def __init__(self, getter, name=None):
            '''
            Construct the cached property.
    
            You may optionally pass in the name that this property has in the
            class; This will save a bit of processing later.
            '''
            self.getter = getter
            self.our_name = name
    
    
        def __get__(self, obj, our_type=None):
    
            if obj is None:
                # We're being accessed from the class itself, not from an object
                return self
    
            value = self.getter(obj)
    
            if not self.our_name:
                if not our_type:
                    our_type = type(obj)
                (self.our_name,) = (key for (key, value) in 
                                    vars(our_type).iteritems()
                                    if value is self)
    
            setattr(obj, self.our_name, value)
    
            return value
    

    For the future, the maintained implementation could probably be found here:

    https://github.com/cool-RR/GarlicSim/blob/master/garlicsim/garlicsim/general_misc/caching/cached_property.py

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is a column that exists in 2 tables. In table 1, this column
There is this strange situation I'm fighting on. I have 3 pages, les call
There is my code that I want to alert test once when mousemove ,
There is a list standard python exceptions that we should watch out, but I
I know there's a lot of other questions out there that deal with this
There is previous little on the google on this subject other than people asking
there is a class that other classes use it in their methods . I
There's this bar that appears at the top, with an app's icon and a
There is a moment in my app, that I need to force to show
There are nice SO question and answers about this issue, but these options didn't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.