• Should NoneType be iterable?

    From Peter Bona@21:1/5 to All on Mon Jun 19 04:49:53 2023
    Hi

    I am wondering if there has been any discussion why NoneType is not iterable My feeling is that it should be.
    Sometimes I am using API calls which return None.
    If there is a return value (which is iterable) I am using a for loop to iterate.

    Now I am getting 'TypeError: 'NoneType' object is not iterable'.

    (Examples are taken from here https://rollbar.com/blog/python-typeerror-nonetype-object-is-not-iterable/)
    Example 1:
    mylist = None
    for x in mylist:
    print(x) <== will raise TypeError: 'NoneType' object is not iterable Solution: extra If statement
    if mylist is not None:
    for x in mylist:
    print(x)


    I think Python should handle this case gracefully: if a code would iterate over None: it should not run any step. but proceed the next statement.

    Has this been discussed or proposed?

    Thanks
    Peter

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Julio Di Egidio@21:1/5 to Peter Bona on Mon Jun 19 05:38:32 2023
    On Monday, 19 June 2023 at 13:50:06 UTC+2, Peter Bona wrote:
    Hi

    I am wondering if there has been any discussion why NoneType is not iterable My feeling is that it should be.
    Sometimes I am using API calls which return None.
    If there is a return value (which is iterable) I am using a for loop to iterate.

    Now I am getting 'TypeError: 'NoneType' object is not iterable'.

    (Examples are taken from here https://rollbar.com/blog/python-typeerror-nonetype-object-is-not-iterable/)
    Example 1:
    mylist = None
    for x in mylist:
    print(x) <== will raise TypeError: 'NoneType' object is not iterable Solution: extra If statement
    if mylist is not None:
    for x in mylist:
    print(x)

    I think Python should handle this case gracefully: if a code would iterate over None: it should not run any step. but proceed the next statement.

    Has this been discussed or proposed?

    I don't know about that, but maybe (also) consider this:

    ```
    for x in mylist or []:
    print(x)
    ```

    which is a rather common pattern with dynamic languages.

    Julio

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Angelico@21:1/5 to python-list@python.org on Tue Jun 20 02:39:19 2023
    On Tue, 20 Jun 2023 at 02:37, Peter Bona via Python-list <python-list@python.org> wrote:

    Hi

    I am wondering if there has been any discussion why NoneType is not iterable My feeling is that it should be.
    Sometimes I am using API calls which return None.
    If there is a return value (which is iterable) I am using a for loop to iterate.

    Now I am getting 'TypeError: 'NoneType' object is not iterable'.

    (Examples are taken from here https://rollbar.com/blog/python-typeerror-nonetype-object-is-not-iterable/)
    Example 1:
    mylist = None
    for x in mylist:
    print(x) <== will raise TypeError: 'NoneType' object is not iterable Solution: extra If statement
    if mylist is not None:
    for x in mylist:
    print(x)


    I think Python should handle this case gracefully: if a code would iterate over None: it should not run any step. but proceed the next statement.

    Has this been discussed or proposed?


    Try this instead:

    for x in mylist or ():

    Now a None list will skip iteration entirely, allowing you to get the
    effect you want :)

    ChrisA

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Julio Di Egidio@21:1/5 to Chris Angelico on Mon Jun 19 10:53:01 2023
    On Monday, 19 June 2023 at 18:39:54 UTC+2, Chris Angelico wrote:
    On Tue, 20 Jun 2023 at 02:37, Peter Bona via Python-list <pytho...@python.org> wrote:

    Hi

    I am wondering if there has been any discussion why NoneType is not iterable My feeling is that it should be.
    Sometimes I am using API calls which return None.
    If there is a return value (which is iterable) I am using a for loop to iterate.

    Now I am getting 'TypeError: 'NoneType' object is not iterable'.

    (Examples are taken from here https://rollbar.com/blog/python-typeerror-nonetype-object-is-not-iterable/)
    Example 1:
    mylist = None
    for x in mylist:
    print(x) <== will raise TypeError: 'NoneType' object is not iterable Solution: extra If statement
    if mylist is not None:
    for x in mylist:
    print(x)


    I think Python should handle this case gracefully: if a code would iterate over None: it should not run any step. but proceed the next statement.

    Has this been discussed or proposed?

    Try this instead:

    for x in mylist or ():

    Now a None list will skip iteration entirely, allowing you to get the
    effect you want :)

    I'd expect the one with the if in front to be the most efficient.

    Yours, unless there is some magic behind the implementation
    of "for" I don't know about, still becomes an iteration over an
    empty iterable, i.e. the same as with "[]". No?

    Or, are you implying that the conversion of () to an empty
    iterable itself is more efficient than that of []? Or, are you
    saying something else altogether?

    Julio

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Neal Becker@21:1/5 to python-list@python.org on Mon Jun 19 14:12:54 2023
    On Mon, Jun 19, 2023 at 12:42 PM Chris Angelico via Python-list < python-list@python.org> wrote:

    On Tue, 20 Jun 2023 at 02:37, Peter Bona via Python-list <python-list@python.org> wrote:

    Hi

    I am wondering if there has been any discussion why NoneType is not
    iterable My feeling is that it should be.
    Sometimes I am using API calls which return None.
    If there is a return value (which is iterable) I am using a for loop to
    iterate.

    Now I am getting 'TypeError: 'NoneType' object is not iterable'.

    (Examples are taken from here
    https://rollbar.com/blog/python-typeerror-nonetype-object-is-not-iterable/
    )
    Example 1:
    mylist = None
    for x in mylist:
    print(x) <== will raise TypeError: 'NoneType' object is not iterable Solution: extra If statement
    if mylist is not None:
    for x in mylist:
    print(x)


    I think Python should handle this case gracefully: if a code would
    iterate over None: it should not run any step. but proceed the next statement.

    Has this been discussed or proposed?


    Try this instead:

    for x in mylist or ():

    Now a None list will skip iteration entirely, allowing you to get the
    effect you want :)

    ChrisA
    --
    https://mail.python.org/mailman/listinfo/python-list

    I prefer iteration of None to be an error, as in my usage it usually
    indicates a mistake that I'd want to catch

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Julio Di Egidio@21:1/5 to All on Mon Jun 19 13:11:04 2023
    On Monday, 19 June 2023 at 21:58:21 UTC+2, dn wrote:
    On 20/06/2023 06.12, Neal Becker via Python-list wrote:

    I prefer iteration of None to be an error, as in my usage it usually indicates a mistake that I'd want to catch

    Agreed!

    That is a *bad practice* with potentially disastrous consequences,
    as I have explained. You too, like Angelico, can't even read?

    A better approach is that the API return (perhaps a tuple of) both
    "status" and "return_value", rather than overloading the latter.

    Which is some pattern for public API's, otherwise just raise an
    error where an error is the case (so called "exceptions").

    That said, apparently the OP use-case is for when there is no interest
    in status/catch, eg where a 'nothing' answer is THE answer.

    A answer, or the question would be moot to begin with.

    Other than that, of course we agree.

    Julio

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Julio Di Egidio@21:1/5 to Neal Becker on Mon Jun 19 12:22:06 2023
    On Monday, 19 June 2023 at 20:47:38 UTC+2, Neal Becker wrote:
    On Mon, Jun 19, 2023 at 12:42 PM Chris Angelico via Python-list <pytho...@python.org> wrote:
    On Tue, 20 Jun 2023 at 02:37, Peter Bona via Python-list <pytho...@python.org> wrote:
    I am wondering if there has been any discussion why NoneType is not
    iterable My feeling is that it should be.
    Sometimes I am using API calls which return None.
    If there is a return value (which is iterable) I am using a for loop to
    iterate.
    Now I am getting 'TypeError: 'NoneType' object is not iterable'. (Examples are taken from here
    https://rollbar.com/blog/python-typeerror-nonetype-object-is-not-iterable/ )
    Example 1:
    mylist = None
    for x in mylist:
    print(x) <== will raise TypeError: 'NoneType' object is not iterable Solution: extra If statement
    if mylist is not None:
    for x in mylist:
    print(x)
    I think Python should handle this case gracefully: if a code would
    iterate over None: it should not run any step. but proceed the next statement.
    Has this been discussed or proposed?
    Try this instead:
    for x in mylist or ():
    Now a None list will skip iteration entirely, allowing you to get the effect you want :)
    I prefer iteration of None to be an error, as in my usage it usually indicates a mistake that I'd want to catch

    That is quite problematic in itself:
    If None is truly invalid for mylist,
    one should report failure, possibly throw,
    immediately after receiving the invalid value.
    Letting it fail later and eventually unpredictably,
    meanS not only useless stack traces, but also,
    and more importantly, that any intervening side
    effects have been effected, with unpredictable
    outcomes, potentially disastrous.

    If None is a legitimate value for mylist, of course
    don't just throw to mimic control flow.

    Julio

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dn@21:1/5 to Neal Becker via Python-list on Tue Jun 20 07:57:39 2023
    On 20/06/2023 06.12, Neal Becker via Python-list wrote:
    On Mon, Jun 19, 2023 at 12:42 PM Chris Angelico via Python-list < python-list@python.org> wrote:

    On Tue, 20 Jun 2023 at 02:37, Peter Bona via Python-list
    <python-list@python.org> wrote:

    Hi

    I am wondering if there has been any discussion why NoneType is not
    iterable My feeling is that it should be.
    Sometimes I am using API calls which return None.
    If there is a return value (which is iterable) I am using a for loop to
    iterate.

    Now I am getting 'TypeError: 'NoneType' object is not iterable'.

    (Examples are taken from here
    https://rollbar.com/blog/python-typeerror-nonetype-object-is-not-iterable/ >> )
    Example 1:
    mylist = None
    for x in mylist:
    print(x) <== will raise TypeError: 'NoneType' object is not iterable >>> Solution: extra If statement
    if mylist is not None:
    for x in mylist:
    print(x)


    I think Python should handle this case gracefully: if a code would
    iterate over None: it should not run any step. but proceed the next
    statement.

    Has this been discussed or proposed?


    Try this instead:

    for x in mylist or ():

    Now a None list will skip iteration entirely, allowing you to get the
    effect you want :)

    ChrisA
    --
    https://mail.python.org/mailman/listinfo/python-list

    I prefer iteration of None to be an error, as in my usage it usually indicates a mistake that I'd want to catch

    Agreed!

    A better approach is that the API return (perhaps a tuple of) both
    "status" and "return_value", rather than overloading the latter.

    That said, apparently the OP use-case is for when there is no interest
    in status/catch, eg where a 'nothing' answer is THE answer.

    --
    Regards,
    =dn

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Igor Berger@21:1/5 to Julio Di Egidio on Mon Jun 19 17:18:50 2023
    On Monday, June 19, 2023 at 4:11:15 PM UTC-4, Julio Di Egidio wrote:
    On Monday, 19 June 2023 at 21:58:21 UTC+2, dn wrote:
    On 20/06/2023 06.12, Neal Becker via Python-list wrote:

    I prefer iteration of None to be an error, as in my usage it usually indicates a mistake that I'd want to catch

    Agreed!
    That is a *bad practice* with potentially disastrous consequences,
    as I have explained. You too, like Angelico, can't even read?

    [snip]

    Julio

    Julio,

    Most of the regulars in this list/group read the posts using the mailing list and
    have declared that they explicitly filter out anything posted on Google Groups.

    I've seen it multiple times with your posts. You respond to something and others post parallel to you.
    It looks like you're being ignored. However, they simply don't see your posts.

    Neither they'll see my response.

    Regards,
    Igor.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Greg Ewing@21:1/5 to All on Tue Jun 20 12:50:36 2023
    I would question the wisdom of designing an API that
    can return either a sequence or None. If it normally
    returns a sequence, and there are no items to return,
    it should return an empty sequence.

    --
    Greg

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Barry@21:1/5 to All on Tue Jun 20 08:36:36 2023
    On 20 Jun 2023, at 01:57, Greg Ewing via Python-list <python-list@python.org> wrote:

    I would question the wisdom of designing an API that
    can return either a sequence or None.

    I have some APIs that do return None or a list.
    The None says that a list is not available and that the caller is
    responsible with dealing in a application-domain specific with
    with that situation.

    In other cases I have API that returns a default list, often [].

    It all depends on the design needs.

    Barry

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Julio Di Egidio@21:1/5 to Igor Berger on Tue Jun 20 02:08:52 2023
    On Tuesday, 20 June 2023 at 02:19:02 UTC+2, Igor Berger wrote:
    On Monday, June 19, 2023 at 4:11:15 PM UTC-4, Julio Di Egidio wrote:
    On Monday, 19 June 2023 at 21:58:21 UTC+2, dn wrote:
    On 20/06/2023 06.12, Neal Becker via Python-list wrote:

    I prefer iteration of None to be an error, as in my usage it usually indicates a mistake that I'd want to catch

    Agreed!

    That is a *bad practice* with potentially disastrous consequences,
    as I have explained. You too, like Angelico, can't even read?

    [snip]

    Most of the regulars in this list/group read the posts using the mailing list and
    have declared that they explicitly filter out anything posted on Google Groups.

    I've seen it multiple times with your posts. You respond to something and others post parallel to you.
    It looks like you're being ignored. However, they simply don't see your posts.

    No, these very same guys did see my posts initially and I bet they still do: rather you don't discount the fact that there are trolls and spammers and
    plain charlatans encroaching this as every community, and I have never
    shied away from calling out these nazi-retarded enemies of life and intelligence. (And YMMV about that, but then don't complain if I call you
    part of the problem).

    That said, I know some people don't read GG and I couldn't care less: I
    write for those who can and do read, and too bad for all the others...

    Julio

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Greg Ewing@21:1/5 to Barry on Wed Jun 21 03:01:00 2023
    On 20/06/23 7:36 pm, Barry wrote:
    I have some APIs that do return None or a list.
    The None says that a list is not available and that the caller is
    responsible with dealing in a application-domain specific with
    with that situation.

    In that case, the caller should probably be checking for
    None rather than blindly trying to iterate over the result.

    --
    Greg

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Angelico@21:1/5 to python-list@python.org on Wed Jun 21 04:12:39 2023
    On Wed, 21 Jun 2023 at 03:46, Igor Berger via Python-list <python-list@python.org> wrote:
    Most of the regulars in this list/group read the posts using the mailing list and
    have declared that they explicitly filter out anything posted on Google Groups.

    I've seen it multiple times with your posts. You respond to something and others post parallel to you.
    It looks like you're being ignored. However, they simply don't see your posts.

    Neither they'll see my response.


    I don't filter out Google Groups. However, the mailing list admins
    have banned certain users, and their posts do not make it across the
    gateway. So for those particular people, NONE of us see their posts.

    If you post on Google Groups and it seems like almost nobody is
    reading your posts, check whether you've been consistently violating
    the Python Code of Conduct. Maybe the problem isn't with everyone
    else.

    ChrisA

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Roel Schroeven@21:1/5 to All on Tue Jun 20 09:30:37 2023
    Op 20/06/2023 om 2:50 schreef Greg Ewing via Python-list:
    I would question the wisdom of designing an API that
    can return either a sequence or None. If it normally
    returns a sequence, and there are no items to return,
    it should return an empty sequence.
    I guess it depends on the reason why there are no items. If it is simply because there are no items, then yes, I agree it should return an empty sequence. But if it is because of some kind of error condition, I don't
    think it should. But in that case I don't think the API should return
    None either: I feel it should raise an exception.

    --
    "Je ne suis pas d’accord avec ce que vous dites, mais je me battrai jusqu’à
    la mort pour que vous ayez le droit de le dire."
    -- Attribué à Voltaire
    "I disapprove of what you say, but I will defend to the death your right to
    say it."
    -- Attributed to Voltaire
    "Ik ben het niet eens met wat je zegt, maar ik zal je recht om het te zeggen tot de dood toe verdedigen"
    -- Toegeschreven aan Voltaire

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Angelico@21:1/5 to python-list@python.org on Wed Jun 21 10:09:18 2023
    On Wed, 21 Jun 2023 at 09:59, Cameron Simpson via Python-list <python-list@python.org> wrote:

    I wasted some time the other evening on an API which returned a string
    or None. My own API, and the pain it caused tells me that that API
    design choice isn't good (it's an automatic attribute based on a tag, returning None if the tag isn't there). My experience so far is that it _looks_ handy so that you can safely say "foo.bar" all the time, but as
    soon a you do something with the value you're in for a world of None-checking.

    https://peps.python.org/pep-0505/

    ChrisA

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Cameron Simpson@21:1/5 to Greg Ewing on Wed Jun 21 09:47:46 2023
    On 21Jun2023 03:01, Greg Ewing <greg.ewing@canterbury.ac.nz> wrote:
    On 20/06/23 7:36 pm, Barry wrote:
    I have some APIs that do return None or a list.
    The None says that a list is not available and that the caller is >>responsible with dealing in a application-domain specific with
    with that situation.

    In that case, the caller should probably be checking for
    None rather than blindly trying to iterate over the result.

    I wasted some time the other evening on an API which returned a string
    or None. My own API, and the pain it caused tells me that that API
    design choice isn't good (it's an automatic attribute based on a tag,
    returning None if the tag isn't there). My experience so far is that it
    _looks_ handy so that you can safely say "foo.bar" all the time, but as
    soon a you do something with the value you're in for a world of
    None-checking.

    I'm rethinking that choice right now. Just the other day I removed a
    setting in a related class which provided an automatic default value
    because, again, while handy for careless use it caused hard to debug
    problems because the default would flow out the call chain until it was unsuitable, making the cause hard to trace.

    And of course I'm very -1 on None acquiring iteration or other features.
    Fail early, fail often!

    Cheers,
    Cameron Simpson <cs@cskk.id.au>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Cameron Simpson@21:1/5 to Chris Angelico on Wed Jun 21 10:44:52 2023
    On 21Jun2023 10:09, Chris Angelico <rosuav@gmail.com> wrote:
    On Wed, 21 Jun 2023 at 09:59, Cameron Simpson via Python-list ><python-list@python.org> wrote:
    I wasted some time the other evening on an API which returned a
    string
    or None. My own API, and the pain it caused tells me that that API
    design choice isn't good (it's an automatic attribute based on a tag,
    returning None if the tag isn't there). My experience so far is that it
    _looks_ handy so that you can safely say "foo.bar" all the time, but as
    soon a you do something with the value you're in for a world of
    None-checking.

    https://peps.python.org/pep-0505/


    Hmm. Thanks. - Cameron Simpson <cs@cskk.id.au>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)