Top and Bottom Values [PEP: 326]

Antoon Pardon apardon at forel.vub.ac.be
Thu Sep 28 03:09:53 EDT 2006


On 2006-09-27, OKB (not okblacke) <brenNOSPAMbarn at NObrenSPAMbarn.net> wrote:
> Antoon Pardon wrote:
>
>> To begin with this already fails:
>> 
>>>>> for i in xrange(Top):
>> ...   print i
>
>     	What do you expect this to do?  Loop forever?

Yes that is what I would expect. If someone would ask me
to implement a function like xrange it would look something
like the following (*)

  def xrange(start=1, stop, step=1):

    while start < stop:
      yield start
      start += step

Since Top is supposed to be bigger than any other value this would
indeed loop forever. The only reason that this doesn't work with
the builtin, is because the builtin xrange insist on its arguments
being ints instead of allowing duck typing.

(*) Yes I know this isn't legal python. I just wrote it like this
to make the intention clear instead of putting in the code that
would actually behave like xrange with regards to its defaults.
IMO that would just detract from the actual code.

-- 
Antoon Pardon



More information about the Python-list mailing list