Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make geometric tensors explicitly not iterable #91

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kalekundert
Copy link
Contributor

Attempting to iterate over a geometric tensor currently results in an infinite loop. The reason is that python considers an object to be iterable if it implements __iter__() or __getitem__(). In the latter case, which is the relevant one for geometric tensors, python iterates by calling __getitem__(i) with incrementing values of i until an IndexError is raised. GeometricTensor.__getitem__() returns an empty tensor instead of raising an IndexError for out-of-bounds indices, so the iteration never completes.

I think it's worth getting rid of the infinite loop behavior, because it can be an unpleasant and confusing surprise for users. There are two ways to do this: (i) don't allow iteration at all, or (ii) mimic the way iteration works for tensors. My preference is for the former. For one thing, raising an error now keeps the door open for supporting iteration later, without breaking backwards compatibility. For another, I don't think that classes should be iterable unless they are truly meant to be containers, and I don't primarily think of tensors as containers.

Signed-off-by: Kale Kundert <kale@thekunderts.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant