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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:14:29+00:00 2026-05-13T18:14:29+00:00

I can’t understand how x and y are the same list. I’ve been trying

  • 0

I can’t understand how x and y are the same list. I’ve been trying to debug it using print statements and import code; code.interact(local=locals()) to drop into various points, but I can’t figure out what on earth is going on 🙁

from collections import namedtuple, OrderedDict

coordinates_2d=["x","y"]

def virtual_container(virtual_container, objects_type):
    """Used to create a virtual object given a the type of container and what it holds.
    The object_type needs to only have normal values."""
    if issubclass(virtual_container, list):
        class my_virtual_container_class:
            """This singleton class represents the container"""
            def __init__(self):
                #Define the default values
                __vals__=OrderedDict([(key,list()) for key in objects_type])
                print(id(__vals__["x"]), id(__vals__["y"]))#ids are different: 12911896 12911968
                #Then functions to access them
                d={key: lambda self: self.__vals__[key] for key in objects_type}
                d["__vals__"]=__vals__
                #Construct a named tuple from this
                self.attr=type('attr_cl',(), d)()
                print(id(self.attr.x()), id(self.attr.y()))#ids are same: 32904544 32904544
            #TODO: Define the operators __del__, setitem, getitem. Also append
        return my_virtual_container_class()

#Nice method of handling coordinates
coordinates=virtual_container(list, coordinates_2d)
x=coordinates.attr.x()
y=coordinates.attr.y()
x.append(1)
y.append(2)
print(x, y)#Prints [1, 2] [1, 2]
  • 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-13T18:14:30+00:00Added an answer on May 13, 2026 at 6:14 pm

    The problem is with this line:

    d={key: lambda self: self.__vals__[key] for key in objects_type}
    

    The lambda uses the value of the variable key, but that value has changed by the time the lambda is called – so all lambdas will actually use the same value for the key.

    This can be fixed with a little trick: Pass the key as a default parameter value to the lambda:

    ... lambda self, key=key: self.__vals__[key] ...
    

    This makes sure that the value of key is bound to the one it had at the time the lambda was created.

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

Sidebar

Related Questions

Can you cast a List<int> to List<string> somehow? I know I could loop through
Can somebody point me to a resource that explains how to go about having
Can anyone (maybe an XSL-fan?) help me find any advantages with handling presentation of
can you recommend some good ASP.NET tutorials or a good book? Should I jump
Can a LINQ enabled app run on a machine that only has the .NET
Can anyone tell me how I can display a status message like 12 seconds
Can you tell me what is the difference between abstraction and information hiding in
Can I get a 'when to use' for these and others? <% %> <%#
Can anyone recommend a good library for generating an audio file, such as mp3,
Can you suggest some good MVC framework for perl -- one I am aware

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.