In a custom object with __iter__()
defined, when calling tuple(obj)
, it returns the data of __iter__()
in tuple form, but I want it to return from __tuple__()
.
This is a really big class, and I don't want to paste it here. I instead wrote a smaller example of my problem.
class Example:
def __init__(self, a, b):
self.data = [a, b]
def __iter__(self):
return iter(reversed(self.data))
def __tuple__(self):
return map(str, self.data)
ex = Example(2, 3)
print([x for x in ex]) # Should return the data from __iter__().
>>> [3, 2] # Reversed, good...
print(tuple(ex)) # Now calling: __tuple__().
>>> (3, 2) # Should have returned ["2", "3"].
If you need to see all of my code, ask, and I'll put it in a Pastebin. I just wanted to save you the effort of sorting through all of those extra methods.
Does __iter__()
override the __tuple__()
type? For some reason I can't get it to change to return what it should from __tuple__()
.
Thanks in advance.