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

  • Home
  • SEARCH
  • 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 8636973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:21:46+00:00 2026-06-12T10:21:46+00:00

Question Is it possible to instantiate an object, set an attribute of that object

  • 0

Question

Is it possible to instantiate an object, set an attribute of that object equal to a class method but delay the calling of that method while enabling access to that attribute (obj.name) without having to call it as a method (obj.name())

Background

I have a class that instantiates an object. Part of that instantiation is setting an attribute equal to a database object, which requires a lookup. This lookup, when instantiating many objects (several hundred), can be slow.

I would like to somehow delay that lookup until that information is needed. However, I don’t want to have to call a method on the object to do that lookup, I would like to simply access the attribute (object.attribute)

Simple Example / What I’ve Tried So Far

class Article(object):

    def __init__(self, id, author):
        self.id = id
        # Note the lack of () after lookup_author below
        self.author = self.lookup_author

        # Temporary holding place for author data
        self.__author = author

    def lookup_author(self):
        # A lookup that would be nice to delay / run as needed
        # Would be something like Author.objects.get(author=self.__author)
        # but set to something simple for this example
        return '<Author: John Doe>'

article1 = Article(1, 'John Doe')

# Returns the bound method
# E.g. <bound method Article.lookup_author of <__main__.Article object at 0x100498950>>
print article1.author

# Calls the method properly, however, you have to use the method calling
# notation of .state() versus .state which is more natural and expected
# for attributes
# Returns <Author: John Doe>
print article1.author()
  • 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-06-12T10:21:47+00:00Added an answer on June 12, 2026 at 10:21 am

    Using properties, you can have article1.author actually call self.lookup_author and return it.

    output:

    John Doe
    <Author: John Doe>
    
    bob
    <Author: bob>
    

    code:

    class Article(object):
        def __init__(self, id, author):
            self.id = id
            self.__author = None
    
        def lookup_author(self):
            return "John Doe"
    
    
        def __str__(self):
            return "<Author: {}>".format(self.author)
    
        @property
        def author(self):
            if self.__author is None:
                self.__author = self.lookup_author()
            return self.__author
    
        @author.setter
        def author(self,name):
            self.__author = name
    
    article1 = Article(1, 'John Doe')
    print "\n", article1.author
    print article1
    
    article1.author = 'bob'
    print "\n", article1.author
    print article1
    

    For some reason, if needed, __author doesn’t have to even exist until the getter is used. You can do that using exceptions.

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

Sidebar

Related Questions

Question Is it possible to stop a Message Driven Bean (programmatically), so that it
Question: Is is possible, with regex, to match a word that contains the same
Question: Is it possible in back end code (not in the code behind but
After a bit of searching, I've found that it's possible to instantiate a WTForms
Is the above question possible? The effect I'm trying to achieve is similar to
Question Is it possible to get the version of the Java Plugin being used
Question: Is it possible to compile a program on linux using a .dll file?
THE QUESTION Is it possible to run one tab from a different Activity or
Short question: Is it possible to detect window.open() in a UIWebView using the UIWebViewDelegate
Possible duplicate question: Is there a way to indefinitely pause a thread? In my

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.