I have the following class:
class UserAccount:
name = None
email = None
password = None
def __str__(self):
return f"{self.name}, {self.email}"
def __repr__(self):
return f"Name: {self.name}, email: {self.email}"
I instatiate the class and call print()
of my created user:
some_user = UserAccount
some_user.name = "John"
some_user.email = "[email protected]"
some_user.password = 'abc'
print(some_user)
When executing the code console returns the following:
<class '__main__.UserAccount'>
…which is equivalent of print(object.__str__(some_user))
output.
I expected the following output:
John, [email protected]
If I use print(some_user.__str__(some_user))
– then it works as expected. But simply passing the user object to print()
invokes the standard __str__
instead of my redefined one, or my redefined __repr__
. What could be the reason for it?
dzelectron is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.