I know for a function, it can return a list term.
def test():
return [1, 2]
value1 = test()[0]
value2 = test()[1]
print(value1) # 1
print(value2) # 2
And I can also return them one by one.
def test():
return 1, 2
value1, value2 = test()
print(value1) # 1
print(value2) # 2
And I really want to know, is there an operator <?> that can unzip list value [1, 2]
to 1, 2
? Such as:
def test():
return <?>[1, 2]
value1 = test()[0]
value2 = test()[1]
print(value1) # 1
print(value2) # 2
The reason I ask this question is that, if I do not know exactly the number of elements in output list, and I still want to let them output one by one.
Before I tried *
operator, but it failed, and the class also not support *
in return function.
Maybe you will think this is not useful. But if we have two classes,
class Box:
...
class Taper:
...
And in another class, we want to return this class object which has several class objects. Then it becomes useful.
class shapes:
def collect_all_shapes(self):
obj = [Box(), Taper()]
return obj
6