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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:47:37+00:00 2026-05-31T02:47:37+00:00

employees = [] for i in range(0,10): emp = Employee(i) emp.first_name = %s-%s%(first name,

  • 0
employees = []
for i in range(0,10):

    emp = Employee(i)
    emp.first_name = "%s-%s"%("first name", i)
    emp.last_name = "%s-%s"%("last_name", i)
    emp.desgination = "%s-%s"%("engineer", i)

    employees.append(emp)


ids = [e.eid for e in employees]

Following is my class definition:

class Employee:

    _fields = {}

    def __init__(self, eid):
        self.eid = eid

    def __getattr__(self, name):
        return self._fields.get(name)

    def __setattr__(self,name,value):
        self._fields[name] = value

    def __str__(self):
        return str(self._fields)

    def __unicode__(self):
        return str(self._fields)

The issue is that when I print ids, it contains 10 times 9… i.e.

[9, 9, 9, 9, 9, 9, 9, 9, 9, 9]

It seems that the same emp variable is being overwritten. I am not sure what going wrong. Though I am a Java coder but I thought I had a fair idea of Python as well.

  • 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-31T02:47:39+00:00Added an answer on May 31, 2026 at 2:47 am

    The problem is, indeed, your java past ! 🙂

    The error is here:

    _fields = {}
    

    _fields is a CLASS member ! So each instance of Employee shares the same _fields var, and whenever you modify one, you modify all other objects.
    You must move the _fields = {} part into the __init__ function :

    self._fields={}
    

    But then, you will run into another problem: self.eid = xx invokes the __setattr__ method! (and so does self._fields !)

    The solution is to use self.__dict__['_fields'] when you need to access to the instance’s ‘_fields’ member instead of self._fields. It will directly access the member, instead of going through __getattr__ and an infinite reccursion, with a nice stack overflow in the end.

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

Sidebar

Related Questions

Let's say I have the following table: Employee name start_date end_date John 2009-10-10 2009-12-31
I have a table with several employees. They have the following columns empid,datecolumn1,is_valid. Very
I have a table with shifts history along with emp ids. I'm using this
I have a table employees and a table department. Each employee is linked to
I have such model and query class Employer(Models.model) name = ... class JobTitle(Models.model) name
Here, the listbox is loaded sample Text. My Model.aspx will be public class Employee
I'm using this query to get all employees of {clients with name starting with
I have a database which stores employees leave. For each day an employee is
I have two models for Publications and Employees: class Publication(models.Model): BOOK_CHAPTER = 1 ARTICLE
Net hires for a date range I have a set of employee data with

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.