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

Can't call next() when iterating through Query; can't iterate through QueryResultIterator #536

Open
jcbgamboa opened this issue Jul 24, 2020 · 1 comment · May be fixed by #655
Open

Can't call next() when iterating through Query; can't iterate through QueryResultIterator #536

jcbgamboa opened this issue Jul 24, 2020 · 1 comment · May be fixed by #655

Comments

@jcbgamboa
Copy link

jcbgamboa commented Jul 24, 2020

(I'm new to PonyORM. I understand I may be asking some nonsense)

I have a table Word, from which I'm selecting:

result = select(w for w in Word)

Then I can iterate through the table with a for loop:

for i in result:
    print(i)

This works. But if I try calling next() on result,

for i in result:
    print(i)
    # skip every second element
    a = next(result)

I get an error:

TypeError: 'Query' object is not an iterator

I thought I'd try calling iter() on result, to create an iterator. Then I can call next(result):

result = iter(result)
a = next(result)
for i in result:  # <-- will crash here
    print(i)

But then I get another error:

TypeError: 'QueryResultIterator' object is not iterable

In other words, this is an iterator that I can't iterate through (?). I am very confused about this problem.

I'm using Python3.6.8, and PonyORM 0.7.13. (let me know if you need more details)

@Zocker1999NET
Copy link

Zocker1999NET commented Jul 2, 2022

The first way you tried is not possible for native Python objects either, e.g. try:

result = list(range(10))
for i in result:
    print(i)
    a = next(result)

However, the second way works on native objects as I know (tested with list). And following example, which then also works, displays only every second entry:

# range(0, 10, 2) in a more complex shape:
result = list(range(10))
result = iter(result)
for i in result:
    print(i)
    a = next(result)

Got the same error as you did in your second example while working with Pony today. I will see if I can create a PR fixing this, should be easy to do.

EDIT: Did not know that before, but implementing the __iter__ method to an Iterator as well (and not just to an Iterable) is even a requirement by Python, so that first calling iter(iterable) and then using it in a for loop is possible without hassle.

Zocker1999NET added a commit to Zocker1999NET/pony that referenced this issue Jul 2, 2022
@Zocker1999NET Zocker1999NET linked a pull request Jul 2, 2022 that will close this issue
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 a pull request may close this issue.

2 participants