Below is the definition for a simple Python class.
class Student:
def __init__(self, name, grade):
self.name = name
self.grade = grade
def __str__(self):
return self.name
As you see, we override the __str__
method to return the student's name. If, for example, I declare a variable x = Student("Trevor", 7)
and subsequently call print(x)
, I see the output Trevor
in the console. Everything is good.
But, if I execute the snippet below, what happens is so dramatically unexpected that it nearly convinced me we're living in a simulation and nothing is real.
s1 = Student("James", 11)
s2 = Student("Charlie", 8)
s3 = Student("Alice", 9)
s4 = Student("Dana", 12)
students = [s1, s2, s3, s4]
print(students)
My Java brain expects the output to be [James, Charlie, Alice, Dana]
, but what I see is [<__main__.Student object at 0x00A7E6D0>, <__main__.Student object at 0x00AFB310>, <__main__.Student object at 0x00AFBB68>, <__main__.Student object at 0x00AFBAD8>]
.
How is this possible? How is anything possible? Am I real? Are you real? I just don't get it. How? Why? WHY?