-1

I have a file called cl.py where I have create a class called Student. Here is the code:

class Student:
    def __init__(self, name, surname):
        self.name = name
        self.surname = surname

I have another file called mat.py where I tried to create an object of that class called student1, the code is this:

from cl import Student

student1 = Student("John", "Baptiste")
print(student1)

The problem is that when I run it on the terminal, I get this:

cl.Student object at 0x04183450

I also get a message that says: "cl could not be resolved ,Pylance(reportMissingImports)"

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 1
    `cl could not be resolved ,Pylance(reportMissingImports)` That sounds like a warning from your IDE. This not an actual Python error. – John Gordon Sep 17 '22 at 22:05

1 Answers1

1

The problem is that when I run it on the terminal, I get this: cl.Student object at 0x04183450

That's normal behavior. When you define a custom class, Python doesn't know anything special about how to print it, so it defaults to showing the class name and the memory address.

If you want it to print in a certain way, define a __str__ method inside the class to display whatever output you like.

John Gordon
  • 29,573
  • 7
  • 33
  • 58