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.

0

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

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?
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'}]

2 Comments

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?
so you want something like: Institute.get(studentid=1) than modify for example trinerName ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.