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:
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?
_______________________________________________