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 7494055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:17:58+00:00 2026-05-29T17:17:58+00:00

How would you implement lazy load of object attributes, i.e. if attributes are accessed

  • 0

How would you implement lazy load of object attributes, i.e. if attributes are accessed but don’t exist yet, some object method is called which is supposed to load these?

My first attempt is

def lazyload(cls):
    def _getattr(obj, attr):
        if "_loaded" not in obj.__dict__:
            obj._loaded=True
            try:
                obj.load()
            except Exception as e:
                raise Exception("Load method failed when trying to access attribute '{}' of object\n{}".format(attr, e))
            if attr not in obj.__dict__:
                AttributeError("No attribute '{}' in '{}' (after loading)".format(attr, type(obj))) # TODO: infinite recursion if obj fails
            return getattr(obj, attr)
        else:
            raise AttributeError("No attribute '{}' in '{}' (already loaded)".format(attr, type(obj)))

    cls.__getattr__=_getattr
    return cls

@lazyload
class Test:
    def load(self):
         self.x=1

t=Test()     # not loaded yet
print(t.x)   # will load as x isnt known yet

I will make lazyload specific to certain attribute names only.
As I havent done much meta-classing yet, I’m not sure if that is the right approach.
What would you suggest?

  • 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-29T17:18:00+00:00Added an answer on May 29, 2026 at 5:18 pm

    Seems like a simple property would do the trick better:

    @property
    def my_attribute():
        if not hasattr(self, '_my_attribute'):
            do_expensive_operation_to_get_attribute()
        return self._my_attribute
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On which technological basis would you implement a multimedia app, which has to meet
I have found this example Lazy load of images in ListView from Fedor which
I would like to implement lazy loading on properties with PostSharp. To make it
So I am trying to implement a basic image lazy load feature to speed
I am trying to implement lazy partitioning of an iterator object that yields slices
I am doing lazy loading and almost done with it. But would like to
I'm having a hard time trying to implement a lazy load for a group
Recently I've been thinking about finite state machines (FSMs), and how I would implement
How would you implement a system with the following objectives: Manage authentication, authorization for
How would I implement a binary search using just an array?

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.