12
votes

I have a class that only contains attributes and I would like packing/unpacking to work on it. What collections.abc should I implement to get this behaviour?

class Item(object):

    def __init__(self, name, age, gender)
        self.name = name
        self.age = age
        self.gender = gender

a, b, c = Item("Henry", 90, "male")

I would like to avoid using a namedtuple.

2
You can unpack any Iterable, so you need to implement __iter__. - jonrsharpe
That depends on whether you want __iter__ to return self or not - you could just return iter((self.name, self.age, self.gender)), for example. It must be iterable, but not necessarily an iterator. - jonrsharpe
Okay thanks I think I got it. - Har

2 Answers

21
votes

You can unpack any Iterable. This means you need to implement the __iter__ method, and return an iterator. In your case, this could simply be:

def __iter__(self):
    return iter((self.name, self.age, self.gender))

Alternatively you could make your class an Iterator, then __iter__ would return self and you'd need to implement __next__; this is more work, and probably not worth the effort.

For more information see What exactly are Python's iterator, iterable, and iteration protocols?


Per the question I linked above, you could also implement an iterable with __getitem__:

def __getitem__(self, index):
    return (self.name, self.age, self.gender)[index]
5
votes

Another option would be a named tuple

Item = collections.namedtuple('Item', ['name', 'age', 'gender']) 

So this works out of the box:

a, b, c = Item("Henry", 90, "male")

If you're on Python 3.7+, you could also take advantage of dataclasses which are more powerful. But you would need to explicitly call astuple:

@dataclasses.dataclass
class Item(object):
    name: str
    age: int
    gender: str

a, b, c = dataclasses.astuple(Item("Henry", 90, "male"))