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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:01:51+00:00 2026-06-13T05:01:51+00:00

class MyOwnClass: # list who contains the queries queries = [] # a template

  • 0
class MyOwnClass:

  # list who contains the queries
  queries = []

  # a template dict
  template_query = {}
  template_query['name'] = 'mat'
  template_query['age'] = '12'

obj = MyOwnClass()

query = obj.template_query
query['name'] = 'sam'
query['age'] = '23'
obj.queries.append(query)

query2 = obj.template_query
query2['name'] = 'dj'
query2['age'] = '19'
obj.queries.append(query2)

print obj.queries

It gives me

[{'age': '19', 'name': 'dj'}, {'age': '19', 'name': 'dj'}]

while I expect to have

[{'age': '23'  , 'name': 'sam'}, {'age': '19', 'name': 'dj'}]

I thought to use a template for this list because I’m gonna to use it very often and there are some default variable who does not need to be changed.

Why does doing it the template_query itself changes? I’m new to python and I’m getting pretty confused.

  • 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-13T05:01:52+00:00Added an answer on June 13, 2026 at 5:01 am

    this is because you are pointing to the same dictionary each time … and overwriting the keys …

    # query = obj.template_query - dont need this
    query = {}
    query['name'] = 'sam'
    query['age'] = '23'
    obj.queries.append(query)
    
    query2 = {} #obj.template_query-dont need this
    query2['name'] = 'dj'
    query2['age'] = '19'
    obj.queries.append(query2)
    

    this should demonstrate your problem

    >>> q = {'a':1}
    >>> lst = []
    >>> lst.append(q)
    >>> q['a']=2
    >>> lst
    [{'a': 2}]
    >>> lst.append(q)
    >>> lst
    [{'a': 2}, {'a': 2}]
    

    you could implement your class differently

    class MyOwnClass:
      # a template dict
      @property
      def template_query():
          return {'name':'default','age':-1}
    

    this will make obj.template_query return a new dict each time

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

Sidebar

Related Questions

Class A { string name; IList<A> minorList = new List<A>(); } IList<A> majorList =
class A{ virtual int foo1(int a){ return foo1_1(a,filler(a)); } template<typename FunctionPtr_filler> int foo1_1(int a,
class Book(models.Model): name = models.CharField(max_length=127, blank=False) class Author(models.Model): name = models.CharField(max_length=127, blank=False) books =
class A(object): _all = set() def __new__(cls): obj = super(A,cls).__new__(cls) cls._all.add(obj) return obj class
class A{ // one to many mapping List<B> listOfB; //getter and setter; class B
class LinkTest{ public: LinkTest(){ } void start(){ List list; Node * n ; for(int
class A { public: A(…) {…} virtual ~A() {…} private: // may contains data
class Person { public int age; public Person() { age = 1; } }
class Widget; std::vector< std::shared_ptr<Widget> > container class Criterium { public: bool operator()(const Widget& left,
Class 1 private void checkDuplicateCustomer(BulkCustomerVO bulkCustomerVO) { PagedDuplicateCustomerVO duplicateCustomerVO = new PagedDuplicateCustomerVO(); duplicateCustomerVO.setCustomer(bulkCustomerVO.getCustomerVO()); duplicateCustomerVO

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.