mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-16 02:27:06 +08:00
implemented __next__ method
This commit is contained in:
11
evaluate.py
11
evaluate.py
@@ -17,7 +17,7 @@ TODO nonlocal statement
|
|||||||
TODO __ instance attributes should not be visible outside of the class.
|
TODO __ instance attributes should not be visible outside of the class.
|
||||||
TODO getattr / __getattr__ / __getattribute__ ?
|
TODO getattr / __getattr__ / __getattribute__ ?
|
||||||
"""
|
"""
|
||||||
from _compatibility import next, property, hasattr
|
from _compatibility import next, property, hasattr, is_py3k
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import itertools
|
import itertools
|
||||||
@@ -939,7 +939,7 @@ def get_scopes_for_name(scope, name_str, position=None, search_global=False):
|
|||||||
try:
|
try:
|
||||||
generators += it.execute_subscope_by_name('__iter__')
|
generators += it.execute_subscope_by_name('__iter__')
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
debug.warning('`for r in x`: x has no __iter__ method')
|
||||||
|
|
||||||
result = []
|
result = []
|
||||||
for gen in generators:
|
for gen in generators:
|
||||||
@@ -948,6 +948,13 @@ def get_scopes_for_name(scope, name_str, position=None, search_global=False):
|
|||||||
# array, but there's also the list builtin, which is
|
# array, but there's also the list builtin, which is
|
||||||
# another thing.
|
# another thing.
|
||||||
in_vars = gen.get_index_types()
|
in_vars = gen.get_index_types()
|
||||||
|
elif isinstance(gen, Instance):
|
||||||
|
# __iter__ returned an instance.
|
||||||
|
name = '__next__' if is_py3k() else 'next'
|
||||||
|
try:
|
||||||
|
in_vars = it.execute_subscope_by_name(name)
|
||||||
|
except KeyError:
|
||||||
|
debug.warning('Instance has no __next__ function', gen)
|
||||||
else:
|
else:
|
||||||
# is a generator
|
# is a generator
|
||||||
in_vars = gen.get_content()
|
in_vars = gen.get_content()
|
||||||
|
|||||||
@@ -62,3 +62,30 @@ next(g)
|
|||||||
g = iter([1.0])
|
g = iter([1.0])
|
||||||
#? float()
|
#? float()
|
||||||
next(g)
|
next(g)
|
||||||
|
|
||||||
|
# -----------------
|
||||||
|
# __next__
|
||||||
|
# -----------------
|
||||||
|
class Counter:
|
||||||
|
def __init__(self, low, high):
|
||||||
|
self.current = low
|
||||||
|
self.high = high
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def next(self):
|
||||||
|
""" need to have both __next__ and next, because of py2/3 testing """
|
||||||
|
return self.__next__()
|
||||||
|
|
||||||
|
def __next__(self):
|
||||||
|
if self.current > self.high:
|
||||||
|
raise StopIteration
|
||||||
|
else:
|
||||||
|
self.current += 1
|
||||||
|
return self.current - 1
|
||||||
|
|
||||||
|
|
||||||
|
for c in Counter(3, 8):
|
||||||
|
#? int()
|
||||||
|
print c
|
||||||
|
|||||||
Reference in New Issue
Block a user