-1

Sorry being a noob, it might be a silly mistake but i am new to python so please help.

I want to have a list of entity in python. For entity, I am using a dictionary. EntityList.py is like this.

from EmployeeEntity import Entity
class AddEmployee:
    def AddEmployeeByUserInput(self):
        EmployeeList = []
        while (input('Do you want to enter an entry y/n:') == 'y'):
            objEmployee = Entity.employee
            objEmployee['id'] = input('Enter id: ')
            objEmployee['fname'] = input('Enter first name: ')
            objEmployee['lname'] = input('Enter last name: ')
            objEmployee['address'] = input('Enter city: ')
            objEmployee['contact'] = input('Enter contact number: ')
            EmployeeList.append(objEmployee)

        print(EmployeeList)



objAddEmployee = AddEmployee()
objAddEmployee.AddEmployeeByUserInput()

EmployeeEntity.py is like this.

class Entity:
employee = {'id':'',\
            'fname':'',\
            'lname':'',\
            'address':'',\
            'contact':''}

This is what i am entering & what i am getting as an output.

Do you want to enter an entry y/n:y
Enter id: 1
Enter first name: Brijesh
Enter last name: Parekh
Enter city: Ahmedabad
Enter contact number: 7545454574
Do you want to enter an entry y/n:y
Enter id: 2
Enter first name: Kandarp
Enter last name: Mehta
Enter city: Bhavnagar
Enter contact number: 3845735375
Do you want to enter an entry y/n:n

[{'id': '2', 'fname': 'Kandarp', 'address': 'Bhavnagar', 'lname': 'Mehta', 'contact': '3845735375'}, {'id': '2', 'fname': 'Kandarp', 'address': 'Bhavnagar', 'lname': 'Mehta', 'contact': '3845735375'}]

My last entered details overwrite all the old ones.

Brij Parekh
  • 11
  • 1
  • 4
  • move the line `EmployeeList = []` outside of the function (for example, before the function definition - but inside the class). The way you implemented it - this list is being re-created upon any call to `AddEmployeeByUserInput()` when what you actually want is to save the *state* even after the function finished its execution. – Nir Alfasi Feb 26 '15 at 06:32
  • At first, i did as you said. Gives the same result. As a class variable, i just have to call self.EmployeeList.append(objEmployee) and print(self.EmployeeList) – Brij Parekh Feb 26 '15 at 06:41
  • And the thing is i am creating and printing the entitylist in the same method only. The state of the variable changes after the method finishes but i am printing the Entitylist in the method scope, which should not cause any state change. I debugged it and found when the loop iterates for the second time, it changes the members in objEmployee and the already appended member in list simultaneously – Brij Parekh Feb 26 '15 at 06:46
  • Thanks AJ for the solution, it works but problem is still there as than there is no meaning of my created entity which i use by 'objEmployee = Entity.employee' sentence. 'Entity' is a class in another python file & 'employee' is the dictionary created. I want to make a list of this entity like we used to create in C# – Brij Parekh Feb 26 '15 at 07:00
  • possible duplicate of [How do I avoid having Python class data shared among instances?](http://stackoverflow.com/questions/1680528/how-do-i-avoid-having-python-class-data-shared-among-instances) – jonrsharpe Feb 26 '15 at 07:44

2 Answers2

1

aftasin answered this in the comments, but i'll put it on here. Move the line EmployeeList = [] outside of the function (for example, before the function definition - but inside the class). The way you implemented it - this list is being re-created upon any call to AddEmployeeByUserInput() when what you actually want is to save the state even after the function finished its execution.

  • Sorry, dude. Doing this doesn't give me the solution i want as I said before in comments. This gives me the same problem. Overwriting the actual list of entity. – Brij Parekh Feb 26 '15 at 07:07
  • This is my entity. class Entity: employee = {'id':'',\ 'fname':'',\ 'lname':'',\ 'address':'',\ 'contact':''} You can create a new '.py' file for it, import the class in another file and write is code with the changes as you said. The problem still remains – Brij Parekh Feb 26 '15 at 07:11
  • I got my answer from here, http://stackoverflow.com/questions/11492656/create-list-of-dictionary-python Using deepcopy, you can append a copy to a list, not a reference. So, new details will not overwrite the old ones because you are adding a copy of it in the list. Thanks for all the help. – Brij Parekh Mar 02 '15 at 09:35
0

Create List of Dictionary Python

Using deepcopy, the copy can be appended, not the reference of the variable.

Community
  • 1
  • 1
Brij Parekh
  • 11
  • 1
  • 4