I will provide an example of the problem in question, in case the title was not clear enough.
Let's say that I have a class Point(object)
that represent 2d coordinates.
Is it possible to create a "magic" method that will allow the following?
x, y = point
Maybe some hacks with iterators?
you can simply tap into the iterator protocol of the object and accomplish this
take a look at http://www.rafekettler.com/magicmethods.html#sequence on more information on how a custom object can be converted into an iterable; or more precisely how one would use an object like an iterable.
Just provide an
__iter__
method.Be careful though. This means your class suddenly becomes safe to use in many other places where it might have previously thrown a type error.
eg.
Ergo, you may want to provide a method other than
__iter__
that provides the iterator.eg.