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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:44:02+00:00 2026-05-20T08:44:02+00:00

In Python, how is it possible to reuse existing equal immutable objects (like is

  • 0

In Python, how is it possible to reuse existing equal immutable objects (like is done for str)? Can this be done just by defining a __hash__ method, or does it require more complicated measures?

  • 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-20T08:44:02+00:00Added an answer on May 20, 2026 at 8:44 am

    If you want to create via the class constructor and have it return a previously created object then you will need to provide a __new__ method (because by the time you get to __init__ the object has already been created).

    Here is a simple example – if the value used to initialise has been seen before then a previously created object is returned rather than a new one created:

    class Cached(object):
        """Simple example of immutable object reuse."""
    
        def __init__(self, i):
            self.i = i
    
        def __new__(cls, i, _cache={}):
            try:
                return _cache[i]
            except KeyError:
                # you must call __new__ on the base class
                x = super(Cached, cls).__new__(cls)
                x.__init__(i)
                _cache[i] = x
                return x
    

    Note that for this example you can use anything to initialise as long as it’s hashable. And just to show that objects really are being reused:

    >>> a = Cached(100)
    >>> b = Cached(200)
    >>> c = Cached(100)
    >>> a is b
    False
    >>> a is c
    True
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible with Python to set the timezone just like this in PHP:
Is it possible for python to accept input like this: Folder name: Download But
Possible Duplicate: Python or Ruby Interpreter on iOS I just discovered this apps pypad
Possible Duplicate: Python “extend” for a dictionary I know that Python list can be
Possible Duplicate: Python: Behaviour of increment and decrement operators Hi, I've tried this. ++num
Possible Duplicate: Python Regular Expression Matching: ## ## I already asked this question, but
Possible Duplicate: python limiting floats to two decimal points If I make this: 12.45-12
Possible Duplicate: Python init and self what do they do? Can anyone explain what
Possible Duplicate: Python : how to append new elements in a list of list?
Possible Duplicate: Python: What is the best way to check if a list is

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.