This has been bugging me for a long time. It seems that python doesn’t have a convention for “Undefined” type. When I started using python I naturally used None for it (as advised). However, the more I work with python, the more I find that None is a meaningful type. And it is a type that is convenient to use in a lot of cases, where it is not actually “None”. One big example is: numpy: array[None, :] Another is simple cascading of defaults. E.g. ```python class A: def __init__(self, default=’string'): self.default = default def method(self, default=None): default = default if default is not None else self.default dct[‘key’] = other_dict.get(‘key’, default) return to_json(dct) ``` None has a logic in `some_func`, which is important. Now if I want to enforce None passed to to_json I have no way to do it. I know there are many workarounds for this, but there is no `consistent` way of doing this. I have been using Ellipsis in the past, but it doesn’t feel right, because it also has a meaning and I might need to use those functions with e.g. numpy in he future and will have to change it. Now, I came back to this issue again and my latest solution is to just define: UNDEFINED = object() in library constants.py Then code above is very clear and I think this is a good and sustainable solution. I know, that in a way ’None’ is already this, but it is generally used in 2 ways: 1. A variable/argument is undefined in python space 2. Global indication, that a value is undefined (e.g. equivalence of null in json) What I am proposing is to have 1 more constant in python ‘Undefined’, which will indicate ‘Undefined’ in python space and leave None to be used in more general sense. I also found this article while looking for solution just now: https://levelup.gitconnected.com/python-why-none-is-not-nothing-bb3de55dd471 It seems that people who use typing are also running into ambiguities and trying to hack something together. I avoid typing until its 99% mature so can't comment more on this. What are your thoughts? Am I onto something or am I missing something? Anyone else had similar encounters?
https://peps.python.org/pep-0661/ On Thu, Jun 8, 2023 at 9:58 AM Dom Grigonis <dom.grigonis@gmail.com> wrote:
This has been bugging me for a long time. It seems that python doesn’t have a convention for “Undefined” type.
When I started using python I naturally used None for it (as advised).
However, the more I work with python, the more I find that None is a meaningful type. And it is a type that is convenient to use in a lot of cases, where it is not actually “None”.
One big example is: numpy: array[None, :]
Another is simple cascading of defaults. E.g.
```python class A: def __init__(self, default=’string'): self.default = default
def method(self, default=None): default = default if default is not None else self.default dct[‘key’] = other_dict.get(‘key’, default) return to_json(dct) ```
None has a logic in `some_func`, which is important. Now if I want to enforce None passed to to_json I have no way to do it.
I know there are many workarounds for this, but there is no `consistent` way of doing this.
I have been using Ellipsis in the past, but it doesn’t feel right, because it also has a meaning and I might need to use those functions with e.g. numpy in he future and will have to change it.
Now, I came back to this issue again and my latest solution is to just define: UNDEFINED = object() in library constants.py
Then code above is very clear and I think this is a good and sustainable solution.
I know, that in a way ’None’ is already this, but it is generally used in 2 ways: 1. A variable/argument is undefined in python space 2. Global indication, that a value is undefined (e.g. equivalence of null in json)
What I am proposing is to have 1 more constant in python ‘Undefined’, which will indicate ‘Undefined’ in python space and leave None to be used in more general sense.
I also found this article while looking for solution just now:
https://levelup.gitconnected.com/python-why-none-is-not-nothing-bb3de55dd471 It seems that people who use typing are also running into ambiguities and trying to hack something together. I avoid typing until its 99% mature so can't comment more on this.
What are your thoughts? Am I onto something or am I missing something? Anyone else had similar encounters?
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/RGUNJY... Code of Conduct: http://python.org/psf/codeofconduct/
-- The dead increasingly dominate and strangle both the living and the not-yet born. Vampiric capital and undead corporate persons abuse the lives and control the thoughts of homo faber. Ideas, once born, become abortifacients against new conceptions.
There's an implementation of a sentinel object in unittest.mock :
from unittest.mock import sentinel sentinel.THING sentinel.THING
On Thu, 8 Jun 2023 at 15:20, David Mertz, Ph.D. <david.mertz@gmail.com> wrote:
https://peps.python.org/pep-0661/
On Thu, Jun 8, 2023 at 9:58 AM Dom Grigonis <dom.grigonis@gmail.com> wrote:
This has been bugging me for a long time. It seems that python doesn’t have a convention for “Undefined” type.
When I started using python I naturally used None for it (as advised).
However, the more I work with python, the more I find that None is a meaningful type. And it is a type that is convenient to use in a lot of cases, where it is not actually “None”.
One big example is: numpy: array[None, :]
Another is simple cascading of defaults. E.g.
```python class A: def __init__(self, default=’string'): self.default = default
def method(self, default=None): default = default if default is not None else self.default dct[‘key’] = other_dict.get(‘key’, default) return to_json(dct) ```
None has a logic in `some_func`, which is important. Now if I want to enforce None passed to to_json I have no way to do it.
I know there are many workarounds for this, but there is no `consistent` way of doing this.
I have been using Ellipsis in the past, but it doesn’t feel right, because it also has a meaning and I might need to use those functions with e.g. numpy in he future and will have to change it.
Now, I came back to this issue again and my latest solution is to just define: UNDEFINED = object() in library constants.py
Then code above is very clear and I think this is a good and sustainable solution.
I know, that in a way ’None’ is already this, but it is generally used in 2 ways: 1. A variable/argument is undefined in python space 2. Global indication, that a value is undefined (e.g. equivalence of null in json)
What I am proposing is to have 1 more constant in python ‘Undefined’, which will indicate ‘Undefined’ in python space and leave None to be used in more general sense.
I also found this article while looking for solution just now:
https://levelup.gitconnected.com/python-why-none-is-not-nothing-bb3de55dd471 It seems that people who use typing are also running into ambiguities and trying to hack something together. I avoid typing until its 99% mature so can't comment more on this.
What are your thoughts? Am I onto something or am I missing something? Anyone else had similar encounters?
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/RGUNJY... Code of Conduct: http://python.org/psf/codeofconduct/
-- The dead increasingly dominate and strangle both the living and the not-yet born. Vampiric capital and undead corporate persons abuse the lives and control the thoughts of homo faber. Ideas, once born, become abortifacients against new conceptions. _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/YTCPDF... Code of Conduct: http://python.org/psf/codeofconduct/
-- Michael Foord Python Consultant, Contractor and Trainer https://agileabstractions.com/
Thank you all for links. Much appreciated.
Begin forwarded message:
From: Michael Foord <fuzzyman@gmail.com> Subject: Re: [Python-ideas] Re: Undefined type Date: 8 June 2023 at 17:41:46 EEST To: david.mertz@gmail.com Cc: Dom Grigonis <dom.grigonis@gmail.com>, python-ideas@python.org
There's an implementation of a sentinel object in unittest.mock :
from unittest.mock import sentinel sentinel.THING sentinel.THING
On Thu, 8 Jun 2023 at 15:20, David Mertz, Ph.D. <david.mertz@gmail.com <mailto:david.mertz@gmail.com>> wrote: https://peps.python.org/pep-0661/ <https://peps.python.org/pep-0661/>
On Thu, Jun 8, 2023 at 9:58 AM Dom Grigonis <dom.grigonis@gmail.com <mailto:dom.grigonis@gmail.com>> wrote: This has been bugging me for a long time. It seems that python doesn’t have a convention for “Undefined” type.
When I started using python I naturally used None for it (as advised).
However, the more I work with python, the more I find that None is a meaningful type. And it is a type that is convenient to use in a lot of cases, where it is not actually “None”.
One big example is: numpy: array[None, :]
Another is simple cascading of defaults. E.g.
```python class A: def __init__(self, default=’string'): self.default = default
def method(self, default=None): default = default if default is not None else self.default dct[‘key’] = other_dict.get(‘key’, default) return to_json(dct) ```
None has a logic in `some_func`, which is important. Now if I want to enforce None passed to to_json I have no way to do it.
I know there are many workarounds for this, but there is no `consistent` way of doing this.
I have been using Ellipsis in the past, but it doesn’t feel right, because it also has a meaning and I might need to use those functions with e.g. numpy in he future and will have to change it.
Now, I came back to this issue again and my latest solution is to just define: UNDEFINED = object() in library constants.py
Then code above is very clear and I think this is a good and sustainable solution.
I know, that in a way ’None’ is already this, but it is generally used in 2 ways: 1. A variable/argument is undefined in python space 2. Global indication, that a value is undefined (e.g. equivalence of null in json)
What I am proposing is to have 1 more constant in python ‘Undefined’, which will indicate ‘Undefined’ in python space and leave None to be used in more general sense.
I also found this article while looking for solution just now: https://levelup.gitconnected.com/python-why-none-is-not-nothing-bb3de55dd471 <https://levelup.gitconnected.com/python-why-none-is-not-nothing-bb3de55dd471> It seems that people who use typing are also running into ambiguities and trying to hack something together. I avoid typing until its 99% mature so can't comment more on this.
What are your thoughts? Am I onto something or am I missing something? Anyone else had similar encounters?
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org <mailto:python-ideas@python.org> To unsubscribe send an email to python-ideas-leave@python.org <mailto:python-ideas-leave@python.org> https://mail.python.org/mailman3/lists/python-ideas.python.org/ <https://mail.python.org/mailman3/lists/python-ideas.python.org/> Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/RGUNJY... <https://mail.python.org/archives/list/python-ideas@python.org/message/RGUNJY...> Code of Conduct: http://python.org/psf/codeofconduct/ <http://python.org/psf/codeofconduct/>
-- The dead increasingly dominate and strangle both the living and the not-yet born. Vampiric capital and undead corporate persons abuse the lives and control the thoughts of homo faber. Ideas, once born, become abortifacients against new conceptions. _______________________________________________ Python-ideas mailing list -- python-ideas@python.org <mailto:python-ideas@python.org> To unsubscribe send an email to python-ideas-leave@python.org <mailto:python-ideas-leave@python.org> https://mail.python.org/mailman3/lists/python-ideas.python.org/ <https://mail.python.org/mailman3/lists/python-ideas.python.org/> Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/YTCPDF... <https://mail.python.org/archives/list/python-ideas@python.org/message/YTCPDF...> Code of Conduct: http://python.org/psf/codeofconduct/ <http://python.org/psf/codeofconduct/>
-- Michael Foord Python Consultant, Contractor and Trainer https://agileabstractions.com/ <https://agileabstractions.com/>
The problem with adding a second None-like type is that gradually it will acquire meaning as a possible value in some contexts. Maybe not in your code, but in other people's. Then will there be a request for a 3rd such object? (4th, 5th ...?). Defining your own sentinel object, as you do, seems like a good solution. It means *you* control what meaning it has in the contexts where it appears. Best wishes Rob Cliffe On 07/06/2023 17:43, Dom Grigonis wrote:
This has been bugging me for a long time. It seems that python doesn’t have a convention for “Undefined” type.
When I started using python I naturally used None for it (as advised).
However, the more I work with python, the more I find that None is a meaningful type. And it is a type that is convenient to use in a lot of cases, where it is not actually “None”.
One big example is: numpy: array[None, :]
Another is simple cascading of defaults. E.g.
```python class A: def __init__(self, default=’string'): self.default = default
def method(self, default=None): default = default if default is not None else self.default dct[‘key’] = other_dict.get(‘key’, default) return to_json(dct) ```
None has a logic in `some_func`, which is important. Now if I want to enforce None passed to to_json I have no way to do it.
I know there are many workarounds for this, but there is no `consistent` way of doing this.
I have been using Ellipsis in the past, but it doesn’t feel right, because it also has a meaning and I might need to use those functions with e.g. numpy in he future and will have to change it.
Now, I came back to this issue again and my latest solution is to just define: UNDEFINED = object() in library constants.py
Then code above is very clear and I think this is a good and sustainable solution.
I know, that in a way ’None’ is already this, but it is generally used in 2 ways: 1. A variable/argument is undefined in python space 2. Global indication, that a value is undefined (e.g. equivalence of null in json)
What I am proposing is to have 1 more constant in python ‘Undefined’, which will indicate ‘Undefined’ in python space and leave None to be used in more general sense.
I also found this article while looking for solution just now: https://levelup.gitconnected.com/python-why-none-is-not-nothing-bb3de55dd471 It seems that people who use typing are also running into ambiguities and trying to hack something together. I avoid typing until its 99% mature so can't comment more on this.
What are your thoughts? Am I onto something or am I missing something? Anyone else had similar encounters?
_______________________________________________ Python-ideas mailing list --python-ideas@python.org To unsubscribe send an email topython-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived athttps://mail.python.org/archives/list/python-ideas@python.org/message/RGUNJY... Code of Conduct:http://python.org/psf/codeofconduct/
unittest.mock.sentinel did the trick for me. As long as there is something for it in a standard library for it I think it’s covered. Everyone defining their own classes or objects would be confusing.
On 12 Jun 2023, at 14:27, Rob Cliffe via Python-ideas <python-ideas@python.org> wrote:
The problem with adding a second None-like type is that gradually it will acquire meaning as a possible value in some contexts. Maybe not in your code, but in other people's. Then will there be a request for a 3rd such object? (4th, 5th ...?). Defining your own sentinel object, as you do, seems like a good solution. It means *you* control what meaning it has in the contexts where it appears. Best wishes Rob Cliffe
On 07/06/2023 17:43, Dom Grigonis wrote:
This has been bugging me for a long time. It seems that python doesn’t have a convention for “Undefined” type.
When I started using python I naturally used None for it (as advised).
However, the more I work with python, the more I find that None is a meaningful type. And it is a type that is convenient to use in a lot of cases, where it is not actually “None”.
One big example is: numpy: array[None, :]
Another is simple cascading of defaults. E.g.
```python class A: def __init__(self, default=’string'): self.default = default
def method(self, default=None): default = default if default is not None else self.default dct[‘key’] = other_dict.get(‘key’, default) return to_json(dct) ```
None has a logic in `some_func`, which is important. Now if I want to enforce None passed to to_json I have no way to do it.
I know there are many workarounds for this, but there is no `consistent` way of doing this.
I have been using Ellipsis in the past, but it doesn’t feel right, because it also has a meaning and I might need to use those functions with e.g. numpy in he future and will have to change it.
Now, I came back to this issue again and my latest solution is to just define: UNDEFINED = object() in library constants.py
Then code above is very clear and I think this is a good and sustainable solution.
I know, that in a way ’None’ is already this, but it is generally used in 2 ways: 1. A variable/argument is undefined in python space 2. Global indication, that a value is undefined (e.g. equivalence of null in json)
What I am proposing is to have 1 more constant in python ‘Undefined’, which will indicate ‘Undefined’ in python space and leave None to be used in more general sense.
I also found this article while looking for solution just now: https://levelup.gitconnected.com/python-why-none-is-not-nothing-bb3de55dd471 <https://levelup.gitconnected.com/python-why-none-is-not-nothing-bb3de55dd471> It seems that people who use typing are also running into ambiguities and trying to hack something together. I avoid typing until its 99% mature so can't comment more on this.
What are your thoughts? Am I onto something or am I missing something? Anyone else had similar encounters?
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org <mailto:python-ideas@python.org> To unsubscribe send an email to python-ideas-leave@python.org <mailto:python-ideas-leave@python.org> https://mail.python.org/mailman3/lists/python-ideas.python.org/ <https://mail.python.org/mailman3/lists/python-ideas.python.org/> Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/RGUNJY... <https://mail.python.org/archives/list/python-ideas@python.org/message/RGUNJY...> Code of Conduct: http://python.org/psf/codeofconduct/ <http://python.org/psf/codeofconduct/>
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/2EH4RS... Code of Conduct: http://python.org/psf/codeofconduct/
participants (4)
-
David Mertz, Ph.D. -
Dom Grigonis -
Michael Foord -
Rob Cliffe