So i want to something like this in Python:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __repr__(self):
return self.name, self.age
p = Person('A', 20)
Then hope to call object p directly to get the tuple (self.name, self.age)
But as you can see when you run this program, you get the problem:
TypeError: __repr__ returned non-string (type tuple)
How can have this behavior?
Thanks!
Note: The problem is not specific to the tuple data type; it can be anything, like a pandas dataframe for example. I just want to return some attribute data, whatever type it is.
(self.name, self.age)as a string, or as an actual tuple?Personclass, or does it also have other methods? If you don't need methods, you could use a namedtuple, or the new dataclass.