Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #106596

Re: Promoting Python

From Marko Rauhamaa <marko@pacujo.net>
Newsgroups comp.lang.python
Subject Re: Promoting Python
Date 2016-04-06 22:22 +0300
Organization A noiseless patient Spider
Message-ID <87wpoak0f7.fsf@elektro.pacujo.net> (permalink)
References (4 earlier) <CAGgTfkPkgKnugcnRaY1PgCuJfUsKv5Ef__rbhTM_oNpK+HTAfQ@mail.gmail.com> <mailman.133.1459949616.32530.python-list@python.org> <87zit6lt8q.fsf@elektro.pacujo.net> <CALwzidnMzeh_qKXBCikrb8T7Mp39+7ERx3QbEf1c1_+NSSaxwA@mail.gmail.com> <mailman.29.1459968672.1197.python-list@python.org>

Show all headers | View raw


Ian Kelly <ian.g.kelly@gmail.com>:

> On Wed, Apr 6, 2016 at 8:14 AM, Marko Rauhamaa <marko@pacujo.net> wrote:
>> Now, if Python had an unlimited range() iterator/iterable, you could use
>> a "for" statement to emulate "while".
>
> You can already do this.
>
>>>> class While:
> ...     def __init__(self, predicate):
> ...         self._predicate = predicate
> ...         self._exited = False
> ...     def __iter__(self):
> ...         return self
> ...     def __next__(self):
> ...         if self._exited or not self._predicate():
> ...             self._exited = True
> ...             raise StopIteration
> ...
>>>> i = 0
>>>> for _ in While(lambda: i < 10):
> ...     print(i)
> ...     i += 1
> ...
> 0
> 1
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9

Impressive!

>> As it stands, Python without "while" could only compute
>> primitive-recursive functions. However, you only need "while" a
>> maximum of one time in your whole program to perform an arbitrary
>> computation.
>
> So this is wrong.

I stand corrected.

However, BartC's No-Buzzword Python doesn't have classes... If he
allowed for types.SimpleNamespace, we could have:

========================================================================
import types

def While(predicate):
    def __iter__():
        return thingy
    def __next__():
        if thingy._exited or not predicate():
            thingy._exited = True
            raise StopIteration
    thingy = types.SimpleNamespace(
        _exited=False, __iter__=__iter__, __next__=__next__)
    return thingy
========================================================================

However, that results in:

    TypeError: 'types.SimpleNamespace' object is not iterable

Where's my bug? Or is CPython buggy? Or is it the documentation:

    The iterator objects themselves are required to support the
    following two methods, which together form the iterator protocol:

    iterator.__iter__()

    [...]

    iterator.__next__()

    <URL: https://docs.python.org/3/library/stdtypes.html#typeiter>

Why is a SimpleNamespace object not an iterator even though it provides
__iter__ and __next__?


Marko

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Promoting Python "Gordon( Hotmail )" <sionet3344@hotmail.co.uk> - 2016-04-05 06:48 +0100
  Re: Promoting Python Rustom Mody <rustompmody@gmail.com> - 2016-04-05 00:31 -0700
    Re: Promoting Python Joel Goldstick <joel.goldstick@gmail.com> - 2016-04-05 08:06 -0400
      Re: Promoting Python alister <alister.ware@ntlworld.com> - 2016-04-05 18:02 +0000
        Re: Promoting Python BartC <bc@freeuk.com> - 2016-04-05 19:47 +0100
          Re: Promoting Python alister <alister.ware@ntlworld.com> - 2016-04-05 19:38 +0000
    Re: Promoting Python Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-04-05 08:13 -0400
      Re: Promoting Python Marko Rauhamaa <marko@pacujo.net> - 2016-04-05 15:31 +0300
  Re: Promoting Python Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-04-06 20:52 +1200
    Re: Promoting Python Marko Rauhamaa <marko@pacujo.net> - 2016-04-06 12:12 +0300
  Re: Promoting Python BartC <bc@freeuk.com> - 2016-04-06 12:06 +0100
    Re: Promoting Python Ned Batchelder <ned@nedbatchelder.com> - 2016-04-06 04:38 -0700
      Re: Promoting Python BartC <bc@freeuk.com> - 2016-04-06 14:21 +0100
    Re: Promoting Python Marko Rauhamaa <marko@pacujo.net> - 2016-04-06 14:46 +0300
      Re: Promoting Python Michael Selik <michael.selik@gmail.com> - 2016-04-06 13:33 +0000
        Re: Promoting Python Marko Rauhamaa <marko@pacujo.net> - 2016-04-06 17:14 +0300
          Re: Promoting Python Chris Angelico <rosuav@gmail.com> - 2016-04-07 00:20 +1000
            Re: Promoting Python Marko Rauhamaa <marko@pacujo.net> - 2016-04-06 21:23 +0300
              Re: Promoting Python Marko Rauhamaa <marko@pacujo.net> - 2016-04-06 21:50 +0300
              Re: Promoting Python Marko Rauhamaa <marko@pacujo.net> - 2016-04-06 22:30 +0300
          Re: Promoting Python Marko Rauhamaa <marko@pacujo.net> - 2016-04-06 22:22 +0300
            Re: Promoting Python Marko Rauhamaa <marko@pacujo.net> - 2016-04-06 22:59 +0300
              Re: Promoting Python Marko Rauhamaa <marko@pacujo.net> - 2016-04-06 23:39 +0300
                Re: Promoting Python Marko Rauhamaa <marko@pacujo.net> - 2016-04-07 01:03 +0300
                Re: Promoting Python Marko Rauhamaa <marko@pacujo.net> - 2016-04-07 09:30 +0300
                Re: Promoting Python Ian Kelly <ian.g.kelly@gmail.com> - 2016-04-07 00:56 -0600
                Re: Promoting Python Marko Rauhamaa <marko@pacujo.net> - 2016-04-07 10:19 +0300
                Re: Promoting Python Steven D'Aprano <steve@pearwood.info> - 2016-04-08 16:09 +1000
          Re: Promoting Python Marko Rauhamaa <marko@pacujo.net> - 2016-04-06 23:05 +0300
      Re: Promoting Python BartC <bc@freeuk.com> - 2016-04-06 14:54 +0100
        Re: Promoting Python Marko Rauhamaa <marko@pacujo.net> - 2016-04-06 17:08 +0300
          Re: Promoting Python Larry Martell <larry.martell@gmail.com> - 2016-04-06 10:36 -0400
        Re: Promoting Python Chris Angelico <rosuav@gmail.com> - 2016-04-07 00:14 +1000
        Re: Promoting Python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-04-06 15:20 +0100
          Re: Promoting Python Ned Batchelder <ned@nedbatchelder.com> - 2016-04-06 07:34 -0700
            Re: Promoting Python Ned Batchelder <ned@nedbatchelder.com> - 2016-04-06 10:55 -0700
              Re: Promoting Python BartC <bc@freeuk.com> - 2016-04-06 23:24 +0100
          Re: Promoting Python BartC <bc@freeuk.com> - 2016-04-06 18:04 +0100
    Re: Promoting Python Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-04-06 08:04 -0400
    Re: Promoting Python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-04-06 13:39 +0100
    Re: Promoting Python Steven D'Aprano <steve@pearwood.info> - 2016-04-07 03:40 +1000

csiph-web