[Jeff Epler] > Is this an example of the elusive "recursive tuple"? It's an example of a tuple subclass; we already have examples of recursive tuples (without subclassing). > class X(tuple): > def __getitem__(self, idx): > if idx == 0: return self > return tuple.__getitem__(self, idx) > > def __len__(self): > return min(1, tuple.__len__(self)) > > >>> x = X([None]) > >>> print len(x), x[0] is x, x==x > 1 True True > >>> print x > (None,) > > I'm also a bit confused by the last line. I guess the builtin > tuple.__repr__ uses the C-level API to access tuple items, not the real > __getitem__ slot? pprint shows the same thing, though. It's off-topic for me. You have the source code <wink>. The key to both is likely what happens when doing for y in x: print y
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4