N
N
Newbieee2021-06-16 21:16:11
Python
Newbieee, 2021-06-16 21:16:11

How to get information from a class object through a dictionary?

class Student

class Student:
    id = ''
    name = ''
    group = ''
    average_score = ''
    def __init__(self, _id, _name, _group, _average_score):
        self.id = _id
        self.name = _name
        self.group = _group
        self.average_score = _average_score


class StudentsSystem
class StudentsSystem:
    Handler = {}
    def __init__(self):
        print("Ready to work with students' data")
    def AddStudent(self, _id, _name, _group, _avgscore):
        self.Handler[_id] = Student(str(_id), str(_name), str(_group), str(_avgscore))
    def PrintInformation(self, _id):
        try:
            print(self.Handler[_id])
        except KeyError:
            print("Wrong child's ID")


Program start point
studentModule = StudentsSystem() # MAIN OBJECT
studentModule.AddStudent("321234312232", "Roh Socko", "9-A", "3.5") 
studentModule.PrintInformation("321234312232") # GET STUDENT'S INFORMATION USING ID


How to get data from objects of the Student class, which is stored in the Handler dictionary? For example, how do I get pseudo_object.name?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-06-16
@Newbieee

I would advise removing print() from the class.
Let your class return a Student object on request, and the calling code already decides what to do with it - print, take separate fields, change ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question