Display class like a tuple in the variables window
I ran the following code in the python console (right click → Run File in Python Console)
class Test:
def __init__(self, *args):
self.values = args
def __iter__(self):
return iter(self.values)
def __repr__(self):
return 'Test(' + ', '.join(map(repr, self.values)) + ')'
def __getitem__(self, index):
return self.values[index]
def __len__(self):
return len(self.values)
t1 = Test(1, 2, 3)
t2 = (1, 2, 3)
Upon completion, my class did not show the indexes in the variables window
Is there a way to make a class display like a tuple without subclassing tuple?
请先登录再写评论。
Hello Chris,
Sure, this is an expected Python behavior: numbers in brackets are not in the tuple actually; they are just passed to the __init__ method as multiple integers. To pass a tuple as an argument, add the required integers to the tuple, like this:
Test((1, 2, 3))
Or, if you want to increase readability, just make a variable with a tuple and pass it as an argument: