Posts: 4,646
Threads: 1,493
Joined: Sep 2016
what value for foo will cause isinstance to return True ?
foo = ???
bar = [1,6,2,5,4,3]
hmm = isinstance(bar,foo)
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 582
Threads: 1
Joined: Aug 2019
>>> foo = type([])
>>> bar = [1,6,2,5,4,3]
>>> hmm = isinstance(bar,foo)
>>> hmm
True
Posts: 4,646
Threads: 1,493
Joined: Sep 2016
my bad. i left out the most important part.
foo = ???
bar = [1,6,2,5,4,3]
bar = iter(bar)
hmm = isinstance(bar,foo) and i don't want to get this by calling type()
i have a function that may get an iterator of any kind. i want to test if it got an iterator.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 582
Threads: 1
Joined: Aug 2019
Very interesting, but I think I do no understand your logic. If you want to check whether an object is iterable, you may use:
>>> hasattr(bar, '__iter__')
True So you don't need foo.
Posts: 4,646
Threads: 1,493
Joined: Sep 2016
i'm looking for the difference between just iterable (a list is iterable) and an iterator, such as (but not limited to) something returned from iter() .
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 4,786
Threads: 76
Joined: Jan 2018
You could test the existence of the __next__ and __iter__ methods.
Posts: 4,646
Threads: 1,493
Joined: Sep 2016
a list has __iter__ but not __next__. so it looks like __next__ is the thing to check. i was thinking of checking for __len__. iterators don't have __len__.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 2,342
Threads: 62
Joined: Sep 2016
from typing import Iterator, Iterable I would suggest using Iterable unless you have a good reason to restrict to Iterators. But both work.
Posts: 4,646
Threads: 1,493
Joined: Sep 2016
i usually have issues like this with functions that i expect to be made public. i like to deal with variations of user input. for example, a function to work with strings, i like to also support bytes. but in many cases i have to do things that require different handling. i've encountered this with iterators in recent weeks.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 7,315
Threads: 123
Joined: Sep 2016
Could run it trough next() ,and catch the in error in try:except .
Then it follow EAFP(easier to ask for forgiveness than permission).
This style is often look up upon as better than LBYL(Look Before You Leap).
Example using isinstance , hasattr or eg os.path.isfile(fname) is LBYL,
not saying this bad in all cases using isfile or isinstance can be more convenient than write a try:except for it.
class NotIterator(Exception):
"""Exception raised when not an iterator"""
pass
def iterator_check(arg):
try:
n = next(arg)
print(n)
except TypeError:
raise NotIterator("Need iterator as input")
if __name__ == '__main__':
lst = [1,6,2,5,4,3]
lst_iter = iter(lst)
# Generator a good wav to make a iterator
numbers = [2, 4, 12, 32, 40, 67]
squares = (n**2 for n in numbers)
iterator_check(lst) Error: During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<module1>", line 18, in <module>
File "<module1>", line 10, in iterator_check
NotIterator: Need iterator as input
|