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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:52:25+00:00 2026-05-15T15:52:25+00:00

Consider this: >>> foo = {} >>> foo[1] = 1.0 >>> foo[2] = foo[1]

  • 0

Consider this:

>>> foo = {}
>>> foo[1] = 1.0
>>> foo[2] = foo[1]
>>> foo
{1: 0.0, 2: 0.0}
>>> foo[1] += 1.0
{1: 1.0, 2: 0.0}

This is what happens. However, what I want would be that the last line reads:

 {1: 1.0, 2: 1.0}

Meaning that both refer to the same value, even when that value changes. I know that the above works the way it does because numbers are immutable in Python. Is there any way easier than creating a custom class to store the value?

  • 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-15T15:52:26+00:00Added an answer on May 15, 2026 at 3:52 pm

    It is possible only with mutable objects, so you have to wrap your immutable value with some mutable object. In fact any mutable object will do, for example built-in list:

    >>> n = [0]
    >>> d = { 1 : n, 2 : n }
    >>> d
    {1: [0], 2: [0]}
    >>> d[1][0] = 3
    >>> d
    {1: [3], 2: [3]}
    

    but what’s hard in creating your own class or object?

    >>> n = type( "number", ( object, ), { "val" : 0, "__repr__" : lambda self: str(self.val) } )()
    >>> d = { 1 : n, 2 : n }
    >>> d
    {1: 0, 2: 0}
    >>> d[1].val = 9
    >>> d
    {1: 9, 2: 9}
    

    Works just as fine 😉

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

Sidebar

Related Questions

Consider a question Which segment of abc.o contains function foo()? Is this the same
Consider this: Function Foo { param( #???? ) } I want to call Foo
Consider this code: class Foo[T : Manifest](val id: String = manifest[T].erasure.getName) I basically want
consider this simple function def foo(l=[]): if not l: print List is empty else
Consider this contrived, trivial example: var foo = new byte[] {246, 127}; var bar
Consider this template function: template<typename ReturnT> ReturnT foo(const std::function<ReturnT ()>& fun) { return fun();
Consider this scenario. I have an object, lets call it.... Foo. Foo raises a
Consider a simple macro: #define ECHO(x) x ECHO(foo(1, 2)) This produces the exact output
Consider this smallest example (I could think of): struct Bar; struct Foo { Bar*
Consider this code: class Foo { public void doIt(String... strs) { System.out.println(this is varargs);

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.