B
B
become_iron2016-03-29 21:30:06
Python
become_iron, 2016-03-29 21:30:06

How to add unboxing for an object of your class?

In Python, you can unpack lists into arguments like this:

a = ['a', 2, 'c']
print(*a)

And how to do exactly the same, but for an object of your custom class?
print(*myObject)
Perhaps some "magic" method needs to be used for this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2016-03-30
@become_iron

For example like this:

class A(object):

    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

    def __iter__(self):
        yield from [self.a, self.b, self.c]


obj = A(1, 2, 3)

print(*obj)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question