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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:18:15+00:00 2026-06-12T17:18:15+00:00

I have a python object that conceptually allows access to an array full of

  • 0

I have a python object that conceptually allows access to an array full of strings through iterators and getters. However, since calculating the exact value of each element in the array is really expensive, I am looking into returning a proxy object for the content of each slot in the array and then calculate on the fly the actual value when it is really needed.

Namely, I would like to write this:

bar = foo.get(10) # just returns a proxy
baz = bar # increase proxy reference
l = [baz] # actually increase proxy reference again.
print baz # ooh, actually need the value. Calculate it only the fly.
v = '%s' % bar # I need the value here again
if bar is None: # I need the value here again
    print 'x'
if bar: # I need the value here again
    print 'x'
for i in bar: # I need the value here again
    print i

In C++, I would try to overload the dereferencing operator… Any idea ?

I understand that for each of these cases, I could overload specific python ‘magic’ functions (such as __str__ for print baz) but I wonder if:

  • this is going to actually cover all possible usecases (are there ways to access the content of a variable that does not involve using a python magic function)
  • there is a more generic way to do this
  • 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-12T17:18:17+00:00Added an answer on June 12, 2026 at 5:18 pm

    In python you’d return a custom type, and override the __str__() method to calculate the string representation at printing time.

    class MyCustomType(object):
        def __str__(self):
            return "My string is really costly to produce"
    

    Depending on your use-cases, you are still looking at the various hooks python provides:

    • Attribute access on custom classes can be hooked into with the __getattr__ method, or by using a property.
    • Accessing individual items in a sequence-like class (list, tuple, string) and mapping-type classes can be hooked into with __getitem__.

    You’ll have to decide, based on your use-case, what you need to hook into, at which point it becomes inevitable that you need to make the expensive calculation. Python will let you hook that almost any point in an object’s lifetime with ease.

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

Sidebar

Related Questions

I want a Python object that will monitor whether other objects have changed since
I have a Python datetime object that I want to convert to unix time,
I have an object that is basically a Python implementation of an Oracle sequence.
I have a Python application in the bottle web-server that accesses a C shared-object
I have a bytearray object in Python 2.7 that I got from a query
I have a Python script that for-loops through some objects and fetches data from
I have a python dictionary object that contains a boolean for every key, e.g.:
I have a JSON object that I am trying to read using Python but
I have a Python object which has certain attributes that are set after the
Does anyone have experience using python to create a COM object that implements some

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.