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 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

Ask A Question

Stats

  • Questions 496k
  • Answers 496k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The . character on the keypad is normally associated to… May 16, 2026 at 11:31 am
  • Editorial Team
    Editorial Team added an answer Use this: <fb:login-button perms="publish_stream, email">Login and Install</fb:login-button> Source: http://developers.facebook.com/docs/guides/web May 16, 2026 at 11:31 am
  • Editorial Team
    Editorial Team added an answer JQuery cookie extension searches the document.cookie attribute to find a… May 16, 2026 at 11:31 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Consider this scenario. I have an object, lets call it.... Foo. Foo raises a
Consider this: One mySQL database that has tables and rows and data within it.
consider this string prison break: proof of innocence (2006) {abduction (#1.10)} i just want
Consider an object declared in a method: public void foo() { final Object obj
Consider the following short code snippet. namespace B { public class Foo { public
Consider the following simple DAG: 1->2->3->4 And a table, #bar, describing this (I'm using
Possible Duplicate: Is there a printf converter to print in binary format? Consider this
When I'm debugging, I sometimes find it useful to replay the last few statements
Consider the following HTML. If I have a JSON reference to the <button> element,
I have the following MXML: <mx:Script> var someBoolean:Boolean = determineSomeCondition(); </mx:Script> .... <foo:MyComponent somePropertyExpectingIDataRenderer={

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.