Let's say I have a following piece of code
(x1, y1), (x2, y2) = foo()
class Bar:
def __init__(self, x, y):
self.x = x
self.y = y
bar1 = Bar(x1, y1)
bar2 = Bar(x2, y2)
Is there any way to avoid having x1
, x2
etc and unpack is directly to Bar instance, e.g. like this:
bar1 = Bar()
bar2 = Bar()
bar1, bar2 = foo()
I would expect some magic method like:
class Bar:
def __init__(self):
self.x = None
self.y = None
def __unpack__(self, x, y):
self.x = x
self.y = y
But there is no such a method (or am I wrong?). How can I accomplish it another way?
Assume that foo
is given and I cannot change it to return objects directly.