Post Aze0jKoBKYtipCRcci by tartley@fosstodon.org
(DIR) More posts by tartley@fosstodon.org
(DIR) Post #AzdiuaDkZNosdUcNUG by DRMacIver@mastodon.social
2025-10-27T15:33:51Z
0 likes, 0 repeats
While we're talking about weird Python edge cases, what do you think this does?```class Foo: def __getitem__(self, i): return i def __len__(self): return 5print(list(Foo()))```
(DIR) Post #Azdiuc0LwAI8AWMhsm by DRMacIver@mastodon.social
2025-10-27T15:35:06Z
0 likes, 0 repeats
The answer is so counter-intuitive to me that I have independently discovered it twice, about a year apart, and posted outraged comments about it in discord both times, and the second time had to be reminded that this was not the first time I'd discovered the behaviour.It's possible it's also not the first time I've posted it on mastodon.
(DIR) Post #Azdiud9bfFHtjVhbwe by cfbolz@mastodon.social
2025-10-27T15:41:44Z
0 likes, 0 repeats
@DRMacIver hmmmmm, if it were the obvious [0, 1, 2, 3, 4] you wouldn't be outraged. so let me guess, it's an infinite loop? a MemoryError?
(DIR) Post #Azdiue3yHgNGYK4cng by DRMacIver@mastodon.social
2025-10-27T15:42:30Z
0 likes, 0 repeats
@cfbolz Correct, yes. It will hang until it eventually runs out of memory.Now, do you know why?
(DIR) Post #Azdiuf1WiG0rX1wBd2 by tartley@fosstodon.org
2025-10-27T15:44:51Z
0 likes, 0 repeats
@DRMacIver @cfbolz Wild guess: the iteration doesn't consult `__len__`, but just keeps retrieving incremental indexes waiting for one to raise an exception.UPDATE: my language is sloppy, but you get what I'm arm-waving about.
(DIR) Post #AzdiufesLxULV4Lbxg by cfbolz@mastodon.social
2025-10-27T15:45:27Z
0 likes, 0 repeats
@tartley @DRMacIver yeah, it probably waits for an IndexError?
(DIR) Post #AzdiugYB2LiyGaDm9w by DRMacIver@mastodon.social
2025-10-27T15:47:47Z
1 likes, 0 repeats
@cfbolz @tartley Yup, exactly that. Default iteration is like:```i = 0while True: try: yield x[i] except IndexError: break i += 1```Instead of:```for i in range(len(x)): yield x[i]```In some sense you should never be able to tell the difference in correct code, so you'll rarely notice this, but I've at least twice managed to write incorrect code that doesn't raise for out of bounds access
(DIR) Post #Aze0jGCSSsLUX2lRGi by cfbolz@mastodon.social
2025-10-27T15:54:28Z
0 likes, 0 repeats
@DRMacIver @tartley I think it's a bit of a general pattern in python, that the language mostly doesn't really use more than one special method in combination (with some exceptions of course)
(DIR) Post #Aze0jHUZf0QmXWFQ92 by cfbolz@mastodon.social
2025-10-27T15:55:15Z
0 likes, 0 repeats
@DRMacIver @tartley also, re the hang: is it actually not even ctrl-c-able? that's *really* annoying?!
(DIR) Post #Aze0jIJyZtY16wITGS by tartley@fosstodon.org
2025-10-27T15:56:17Z
0 likes, 0 repeats
@cfbolz @DRMacIver oh darn I hadn't expected that. I confess I don't know what determines that. Now you've nerd sniped me into figuring that out...
(DIR) Post #Aze0jJM8jKsAJwJiHA by dabeaz@mastodon.social
2025-10-27T16:25:02Z
0 likes, 0 repeats
@tartley @cfbolz @DRMacIver It's probably running a tight loop in some C code somewhere outside of the main eval loop where ctrl-c checking happens.
(DIR) Post #Aze0jK5rzJS6bliEYS by cfbolz@mastodon.social
2025-10-27T16:27:27Z
0 likes, 0 repeats
@dabeaz @tartley @DRMacIver nope, it was actually my own fault. ctrl-c works fine in CPython, but actually not in pypy3 😬 (it's my default python, so I tried there)
(DIR) Post #Aze0jKoBKYtipCRcci by tartley@fosstodon.org
2025-10-27T16:28:03Z
0 likes, 0 repeats
@cfbolz @dabeaz @DRMacIver The eternal programmer's refrain!
(DIR) Post #Aze0jLf0AB9HT19nxA by cfbolz@mastodon.social
2025-10-27T16:32:38Z
0 likes, 0 repeats
@tartley @dabeaz @DRMacIver I mean, you can definitely get CPython to enter an uninterrupted loop with the approach below. you get bonus points for telling me why operator.index works as `__getitem__` despite not taking a self?! (I don't know the answer to that). and why ctrl-c doesn't work here.
(DIR) Post #Aze0jMNJVQatgRtC1Q by dabeaz@mastodon.social
2025-10-27T16:46:38Z
0 likes, 0 repeats
@cfbolz @tartley @DRMacIver Whoa. That's freaky. I don't think I've ever seen the __index__ method before. Couldn't tell you want it does or what purpose it serves.
(DIR) Post #Aze0jN2R2XUHjz827M by cfbolz@mastodon.social
2025-10-27T16:55:58Z
0 likes, 0 repeats
@dabeaz @tartley @DRMacIver anyway, it's a super old feature, from python 2.5 times: https://peps.python.org/pep-0357/ (so you likely saw it at some point and suppressed the memory ;-))
(DIR) Post #Aze0jNmsFsdO40r7VA by cfbolz@mastodon.social
2025-10-27T17:06:49Z
0 likes, 0 repeats
@dabeaz @tartley @DRMacIver bit mean, but it's in section '4.12 Conversion Protocols' of Python Distilled, no? 😬😂
(DIR) Post #Aze0jOqoIjNRMVhmHA by dabeaz@mastodon.social
2025-10-27T17:31:05Z
0 likes, 0 repeats
@cfbolz @tartley @DRMacIver Could be. I'll refer to my PTSD comment from earlier). Arg. The surface area is too damn big and there are corner cases everywhere!
(DIR) Post #Aze0jPY3hvyJWdwJge by dabeaz@mastodon.social
2025-10-27T17:34:53Z
0 likes, 0 repeats
@cfbolz @tartley @DRMacIver Since we're on the general topic of WTF?!? Here's one of my favorite examples to show and discuss in class:
(DIR) Post #Aze0jQ7Va8KPIaWcwS by cfbolz@mastodon.social
2025-10-27T17:54:30Z
0 likes, 0 repeats
@dabeaz @tartley @DRMacIver wow, that's amazing/horrible! NamedTuple isn't even a class at all, but a function?!
(DIR) Post #Aze0jQraonBvbW5Qm0 by pozorvlak@mathstodon.xyz
2025-10-27T19:14:32Z
1 likes, 0 repeats
@cfbolz @dabeaz @tartley @DRMacIver Interestingly/horribly, this hasn't always been the case: the first Python interpreter I tried this on was Python 3.7, when typing.NamedTuple was a class. The isinstance call still returned False, of course, because everything about named tuples is cursed.
(DIR) Post #Aze0jRpVE376bK7H9c by dabeaz@mastodon.social
2025-10-27T19:17:49Z
1 likes, 0 repeats
@pozorvlak @cfbolz @tartley @DRMacIver Wait, NamedTuple started as a class and they changed it to a function? Good god.
(DIR) Post #Aze0jTIblJzP9sk29w by DRMacIver@mastodon.social
2025-10-27T19:32:27Z
1 likes, 0 repeats
@dabeaz @pozorvlak @cfbolz @tartley I'm really enjoying the amount of Python developer trauma bonding that is occurring here today.
(DIR) Post #Aze0jVm6YclwpwYcPg by cfbolz@mastodon.social
2025-10-27T17:57:55Z
0 likes, 0 repeats
@dabeaz @tartley @DRMacIver collections.namedtuple has long been a private enemy of PyPy because it used to use really bad anti-patterns in the way it does meta programming. Nowadays it's fixed I think, but for a couple of CPython releases we were basically constantly annoyed with that code
(DIR) Post #Aze0q1qgDkTw37nydM by DRMacIver@mastodon.social
2025-10-27T17:57:03Z
0 likes, 0 repeats
@cfbolz @dabeaz @tartley I knew NamedTuple was a function, so I was staring at this for a while trying to figure out how this could possibly work, and then decided maybe best if I just don't know.But goddammit now I'm curious again.
(DIR) Post #Aze0q3LCfkUYg55rqi by cfbolz@mastodon.social
2025-10-27T18:01:14Z
0 likes, 0 repeats
@DRMacIver @dabeaz @tartley inheriting from something that's not a type just means calling it, I think. That's definitely in the 'weird corner cases' category again though,so I don't remember the details 😬
(DIR) Post #Aze0q4NMpBoht576rQ by DRMacIver@mastodon.social
2025-10-27T18:04:02Z
1 likes, 0 repeats
@cfbolz @dabeaz @tartley It appears that what's happening is that it calls `__mro_bases__()` on it.
(DIR) Post #Aze0q87usZpnWJupKi by DRMacIver@mastodon.social
2025-10-27T17:59:56Z
0 likes, 0 repeats
@cfbolz @dabeaz @tartley OK I've looked up just enough to know I don't want to know more.
(DIR) Post #Aze0rz8xFXzoEqHmPw by tintvrtkovic@mastodon.social
2025-10-27T18:07:30Z
0 likes, 0 repeats
@dabeaz @cfbolz @tartley @DRMacIver hold on, this works nowadays? I remember having to commit crimes to get namedtuples to work in cattrs
(DIR) Post #Aze0s07ZcAU9GqeBu4 by dabeaz@mastodon.social
2025-10-27T18:11:23Z
1 likes, 0 repeats
@tintvrtkovic @cfbolz @tartley @DRMacIver I think this NamedTuple inheritance thing has "worked" since at least 3.6.
(DIR) Post #Aze0sPKtrl03TQ4ge8 by dabeaz@mastodon.social
2025-10-27T17:59:56Z
0 likes, 0 repeats
@cfbolz @tartley @DRMacIver One of the things I love about this is asking students what they think the answer ought to be first and maybe taking a poll before revealing anything.
(DIR) Post #Aze0sQOTvvSWkol3rs by cfbolz@mastodon.social
2025-10-27T18:03:37Z
0 likes, 0 repeats
@dabeaz @tartley @DRMacIver heh, that's a slightly mean exercise.My main question is why we still use named tuples at all, now that data classes are a thing
(DIR) Post #Aze0sRFIlXi5OdTFCK by cfbolz@mastodon.social
2025-10-27T18:13:42Z
1 likes, 0 repeats
@dabeaz @tartley @DRMacIver (also, why are there namedtuples and structseq in python, they do almost the same thing)
(DIR) Post #Aze0y2avKcBegBFuZk by cfbolz@mastodon.social
2025-10-27T19:20:40Z
0 likes, 0 repeats
@dabeaz @pozorvlak @tartley @DRMacIver yeah, it switched from a class to a function in 3.9
(DIR) Post #Aze0y3xeFbxUuwtZdQ by dabeaz@mastodon.social
2025-10-27T19:30:10Z
1 likes, 0 repeats
@cfbolz @pozorvlak @tartley @DRMacIver Won't lie--this change from a class to a function makes me love it even more. Not as a feature, but as a discussion topic.