Promoting Python
Ian Kelly
ian.g.kelly at gmail.com
Wed Apr 6 15:40:58 EDT 2016
On Wed, Apr 6, 2016 at 1:22 PM, Marko Rauhamaa <marko at pacujo.net> wrote:
> 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__?
Because Python expects those methods to be defined in the class dict,
not the instance dict. In a world with only SimpleNamespace and no
classes, I think we could reasonably expect that to work (assuming
that the iterator protocol is even still a thing in that world).
More information about the Python-list
mailing list