0

I need to perform some CRUD operations in Python.

I have used below details. I have captured some users details and added them in a list now, where I'm printing that list I am only getting last inserted row.

from pprint import pprint


class Institute:
    studentID = int();
    studentName=str();
    trinerName=str();

my_list = []

my_obj = Institute()
my_obj.studentID = 100;
my_obj.studentName = "Student 1";
my_obj.trinerName = "Trainer 1";

my_list.append(my_obj)


my_obj.studentID = 101; 
my_obj.studentName = "Student 2";
my_obj.trinerName = "Trainer 2";

my_list.append(my_obj)


newobj = vars(my_obj);

print(newobj);  

This gives me

{'studentID': 101, 'studentName': 'Student 2', 'trinerName': 'Trainer 2'}                                                             

But I want to display all added objects.

halfer
  • 19,824
  • 17
  • 99
  • 186
Passionate Coder
  • 7,154
  • 2
  • 19
  • 44

2 Answers2

2

You're using the same object for each element of the list. You need to create a new object each time.

And at the end you need to print the whole list, not just the last object.

from pprint import pprint
class Institute:
    studentID = int();
    studentName=str();
    trinerName=str();

my_list = []

my_obj = Institute()
my_obj.studentID = 100;
my_obj.studentName = "Student 1";
my_obj.trinerName = "Trainer 1";

my_list.append(my_obj)

my_obj = Institute()
my_obj.studentID = 101; 
my_obj.studentName = "Student 2";
my_obj.trinerName = "Trainer 2";

my_list.append(my_obj)

newobj = list(map(vars, my_list))

print(newobj);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks for your answer can you tell me one more thing if i want to update data for some StudentId let studentID = 101 i want to update some of value tha how can i get index of it in above case it is 1. How can i get it dynmically? – Passionate Coder Sep 13 '19 at 08:51
  • See https://stackoverflow.com/questions/7125467/find-object-in-list-that-has-attribute-equal-to-some-value-that-meets-any-condi – Barmar Sep 13 '19 at 09:01
2

In your case, using instance variables is cleaner than using class variables.

Instance variables are variables used for data that is unique to a particular instance.

Class variables are variables that are shared by all instances of a class.

from pprint import pprint
class Institute:
    def __init__(self, studentID, studentName, trinerName):
        self.studentID = studentID
        self.studentName = studentName
        self.trinerName = trinerName

my_list = []

my_obj = Institute(100, "Student 1", "Trainer 1")
my_list.append(my_obj)

my_obj = Institute(101, "Student 2", "Trainer 2")
my_list.append(my_obj)


all_new_obj = list(map(vars, my_list))

pprint(all_new_obj)

output:

[{'studentID': 100, 'studentName': 'Student 1', 'trinerName': 'Trainer 1'},
 {'studentID': 101, 'studentName': 'Student 2', 'trinerName': 'Trainer 2'}]
kederrac
  • 16,819
  • 6
  • 32
  • 55
  • Thanks for your answer can you tell me one more thing if i want to update data for some StudentId let studentID = 101 i want to update some of value tha how can i get index of it in above case it is 1. How can i get it dynmically? – Passionate Coder Sep 13 '19 at 08:51
  • so you want something like: Institute.get(studentid=1) than modify for example trinerName ? – kederrac Sep 13 '19 at 08:54