0

python string to class

here, i want to take the input and print out the student name.... so, is it possible to convert a string to a class type? class student: def __init__(self, name, age, grade): self.name = name self.age = age self.grade = grade def say_hi(self): return f"hi, i am {self.name}, i am {self.age} and my grade for last was {self.grade}" jaff = student('jaff', 18, 75) bill = student('bill', 17, 95) student_id = input("who would you like to call up: ") print(student_id.say_hi())

10th Aug 2021, 11:35 PM
Tomoe
Tomoe - avatar
2 ответов
+ 2
I don't think there is a way(to my knowledge, I could definitely be wrong), but to solve your problem you could maybe use a dictionary and use the names as the key, the object as the value, then you can search for the name as a key, and then you have the object. It might also make it easier to understand when looking at just the code. For example: students = {"jaff" : jaff, "bill" : bill} print(students[student_id].say_hi()) Hope this helps!
10th Aug 2021, 11:47 PM
Chloe
Chloe - avatar
0
Try this import ctypes class Student: ... st1 = Student(...) ref = id(st1) deref=ctypes.cast(ref, ctypes.py_object).value you could also save the id in class itself with: self.id = id(self)
11th Aug 2021, 12:46 AM
Abs Sh
Abs Sh - avatar