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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:23:19+00:00 2026-06-04T02:23:19+00:00

I have a question regarding the usage of self variable in Python. Please look

  • 0

I have a question regarding the usage of self variable in Python. Please look at the following example:

from copy import deepcopy
class IntClass:
    props = {}
    def __init__(self, keys, values):
        indx = 0
        for key in keys:
            self.props[key] = values[indx]
            indx += 1
def display(self):
    for key in self.props.keys():
        print 'key=%s value=%s' %(key,self.props[key])

class IntGen:
    def gen(self, keys, values):
        for vs in values:
            yield [keys, vs]

    def start(self, keys, values):
        self.loader = self.gen(keys, values)

    def nextItem(self):
        return self.loader.next()

keys = ['k1', 'k2', 'k3']
values = [['v1.1', 'v1.2', 'v1.3'], ['v2.1', 'v2.2', 'v2.3'], ['v3.1', 'v3.2', 'v3.3']]

holder = []
intGen = IntGen()
intGen.start(keys, values)
while True:
    try:
        a = intGen.nextItem()
        holder.append(deepcopy(IntClass(a[0],a[1])))
    except StopIteration:
        break

for h in holder:
    h.display()

The result, as in my understanding, should be:

key=k3 value=v3.3
key=k2 value=v3.2
key=k1 value=v3.1
key=k3 value=v2.3
key=k2 value=v2.2
key=k1 value=v2.1
key=k3 value=v1.3
key=k2 value=v1.2
key=k1 value=v1.1

However, what I got is as follows:

key=k3 value=v3.3
key=k2 value=v3.2
key=k1 value=v3.1
key=k3 value=v3.3
key=k2 value=v3.2
key=k1 value=v3.1
key=k3 value=v3.3
key=k2 value=v3.2
key=k1 value=v3.1

It seems to me that within the While loop, when I try to create a new instance of IntClass, that new instance has modified the values stored in props attribute of the instances created in the previous loop, which at the end, cause the holder contains all instances with the same data.

Anyone can point me to the answer? Look like there is something wrong with self variable but I have no glue on how to fix it.

Thanks a lot,

  • 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-04T02:23:20+00:00Added an answer on June 4, 2026 at 2:23 am

    You put the line props = {} directly in the class definition. This will cause all instances of the class to share the same dictionary.

    If you want each instance to have its own dictionary, put self.props = {} in __init__ instead.

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

Sidebar

Related Questions

Good afternoon, I have a quick 'top level' question regarding the usage of Facebook
I have a specific question regarding the usage of profiler. I am new to
I have a question regarding the usage of functions in a command in bash.
I am going over C and have a question regarding const usage with pointers.
I have a question regarding the usage of using statements around ObjectContext objects in
I have a question regarding parsing data from Wikipedia for my Android app. I
I have a question regarding calling methods from different threads. Well I am using
I have a few basic questions regarding the syntax and usage of properties in
I have question regarding the SQLAlchemy. How can I add into my mapped class
I have question regarding the use of function parameters. In the past I have

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.