Why do i get an error

class Student: def __init__(self, input_name, input_section, input_age, input_nationality, input_class , input_gpa, input_weight, input_height, input_school) : self.name = input_name self.section = input_section self.year = input_class self.age = input_age self.nationality = input_nationality self.gpa = input_gpa self.weight = input_weight self.height = input_height self.school = input_school def __repr__(self): print("student name : "+ self.name) print("student section : "+ self.section) print("student class : "+ self.year) print("student nationality : " + self.nationality) print("student GPA : " + str(self.gpa)) print("student weight : "+str(self.weight)) print("student height : "+str(self.height)) print("school : "+ self.school) print(Student("Sarah","B","14","American","9","5","50","153","high"))

Why do i get an error

__repr__ is supposed to return a string. You are printing strings in __repr__ instead of returning a string.

from datetime import datetime

d = datetime.now()

print(d.__str__())  
# Output: 2022-12-25 11:50:52.689663
print(type(d.__str__())) 
# Output: <class 'str'>

print(d.__repr__()) 
# Output: datetime.datetime(2022, 12, 25, 11, 50, 52, 689663)
print(type(d.__repr__())) 
# Output: <class 'str'>

See documentation: object.__repr__(self)

1 Like