Issue
I want print information about my Object when I use print function!
if I do print( my_product ) it displays Product(name = the_name).
My Class:
class Product:
def __init__(self, name = ""):
self._name = name
@property
def name(self):
return self._name
e.g.:
my_product = Product("Computer")
print(my_product)
#Product(name=Computer)
Can you help me please ?
Solution
You need to define a __str__
function for the class like so:
class Product:
def __init__(self, name = ""):
self._name = name
@property
def name(self):
return self._name
def __str__(self):
return " Product(name = " + self._name + ")"
Answered By - BTables
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.