Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'great.': 0.07; 'converts': 0.09; 'method,': 0.09; 'present,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'sure,': 0.09; 'val': 0.09; 'def': 0.12; 'jan': 0.12; 'adapter': 0.16; 'iterator': 0.16; 'iterators': 0.16; 'loops': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'xrange': 0.16; 'wrote:': 0.18; 'pointed': 0.19; 'header:User-Agent:1': 0.23; 'looks': 0.24; 'class.': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'raise': 0.29; 'originally': 0.30; 'start,': 0.30; 'then.': 0.30; 'work.': 0.31; 'class': 0.32; 'implemented': 0.33; 'skip:_ 10': 0.34; 'except': 0.35; 'objects': 0.35; 'but': 0.35; "didn't": 0.36; 'method': 0.36; 'positive': 0.37; 'step': 0.37; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'ian': 0.60; 'introduced': 0.61; 'back': 0.62; 'received:fios.verizon.net': 0.84; 'stop,': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: l = range(int(1E9)) Date: Sat, 02 May 2015 19:51:58 -0400 References: <87k2wtvbx1.fsf@Equus.decebal.nl> <_M61x.477467$Ek.357048@fx07.am4> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-98-114-97-173.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430610731 news.xs4all.nl 2955 [2001:888:2000:d::a6]:43768 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:89816 On 5/2/2015 5:31 PM, Ian Kelly wrote: > Would it have been better if range() had been implemented as xrange() > from the beginning? Sure, that would have been great. Except for one > small detail: the iterator protocol didn't exist back then. For loops originally used the getitem iterator protocol. xrange objects have a __getitem__ method, but not __iter__ or __next__. As Mark pointed out, they were introduced in 1993. Getitem iterators still work. If iter(ob) is passed object ob without .__iter__, iter looks for ob.__getitem__ and if present, returns iterator(ob), where iterator is an internal protocol adapter class. Its __next__ method calls ob.__getitem__ and converts IndexError to StopIteration. In 3.4.3: class xrange: def __init__(self, start, stop, step): if step < 1: raise ValueError('incomplete xrange require positive step') self.val = start self.stop = stop self.step = step def __getitem__(self, dummy): val = self.val self.val = val + self.step if val < self.stop: return val else: raise IndexError('') it = iter(xrange(1, 10, 3)) print(type(it)) for i in it: print(i) # 1 4 7 -- Terry Jan Reedy