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?
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?
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 :)
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 notiterable My feeling is that it should be.
Sometimes I am using API calls which return None.iterate.
If there is a return value (which is iterable) I am using a for loop to
Now I am getting 'TypeError: 'NoneType' object is not iterable'.
(Examples are taken from herehttps://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 woulditerate 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
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!
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.
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 prefer iteration of None to be an error, as in my usage it usually indicates a mistake that I'd want to catch
I am wondering if there has been any discussion why NoneType is notiterable My feeling is that it should be.
Sometimes I am using API calls which return None.iterate.
If there is a return value (which is iterable) I am using a for loop to
Now I am getting 'TypeError: 'NoneType' object is not iterable'. (Examples are taken from herehttps://rollbar.com/blog/python-typeerror-nonetype-object-is-not-iterable/ )
Example 1:iterate over None: it should not run any step. but proceed the next statement.
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
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 :)
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-listI prefer iteration of None to be an error, as in my usage it usually indicates a mistake that I'd want to catch
<python-list@python.org> wrote:
iterable My feeling is that it should be.
Hi
I am wondering if there has been any discussion why NoneType is not
Sometimes I am using API calls which return None.iterate.
If there is a return value (which is iterable) I am using a for loop to
https://rollbar.com/blog/python-typeerror-nonetype-object-is-not-iterable/ >> )
Now I am getting 'TypeError: 'NoneType' object is not iterable'.
(Examples are taken from here
Example 1:iterate over None: it should not run any step. but proceed the next
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
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
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
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.
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.
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.
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 would question the wisdom of designing an API thatI 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
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 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.
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.
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 ahttps://peps.python.org/pep-0505/
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.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 300 |
Nodes: | 16 (2 / 14) |
Uptime: | 75:03:07 |
Calls: | 6,716 |
Calls today: | 4 |
Files: | 12,246 |
Messages: | 5,357,385 |