[Python-Dev] Clean way in python to test for None, empty, scalar, and list/ndarray? A prayer to the gods of Python
Brett Cannon
brett at python.org
Fri Jun 14 22:03:41 CEST 2013
On Fri, Jun 14, 2013 at 3:12 PM, Martin Schultz <maschu09 at gmail.com> wrote:
> As much as I love python, the following drives me crazy, and I would wish
> that some future version would come up with a more consistent approach for
> this. And please don't reply with "Too bad if you don't know what type your
> data are..." - if I want to implement some generic functionality, I want to
> avoid constrains on the user where not absolutely necessary, and I believe
> this approach is truely pythonic.
>
> OK - here comes the problem set. These are in fact several related issues.
> Clearly, a solution exists for each of them, but you will have to admit
> that they are all very different in style and therefore unnecessarily
> complicate matters. If you want to write code which works under many
> circumstances, it will become quite convoluted.
>
> 1. Testing for None:
>
> From what I read elsewhere, the preferred solution is `if x is None` (or
> `if not x is None` in the opposite case). This is fine and it works for
> scalars, lists, sets, numpy ndarrays,...
>
Should actually be ``if x is not None``.
[SNIP]
>
> - add a good `isiterable` function
>
Done: isinstance(x, collections.abc.Iterable)
> - add a `size` attribute to all objects (I wouldn't mind if this is None
> in case you don't really know how to define the size of something, but it
> would be good to have it, so that `anything.size` would never throw an error
>
This is what len() is for. I don't know why numpy doesn't define the
__len__ method on their array types for that. We can't force every object
to have it as it doesn't make sense in all cases.
> - add an `isscalar` function which would at least try to test if
> something is a scalar (meaning a single entity). Note that this might give
> different results compared to `isiterable`, because one would consider a
> scalar string as a scalar even though it is iterable. And if `isscalar`
> would throw exceptions in cases where it doesn't know what to do: fine -
> this can be easily captured.
>
The numbers module has a bunch of ABCs you can use to test for integrals,
etc. just as I suggested for iterables.
> - enable the `len()` function for scalar variables such as integers or
> floats. I would tend to think that 1 is a natural answer to what the length
> of a number is.
>
The len() function is specifically for containers so it would not make
sense to ask for the length of something you can't put something into or
iterate over.
This is actually a perfect case of using the new single-dispatch generic
function work that has landed in Python 3.4 (
http://docs.python.org/3.4/library/functools.html#functools.singledispatch).
With that you could write your own custom_len() function that dispatches on
the type to return exactly what you are after. E.g.::
@functools.singledispatch
def custom_length(ob): return len(ob)
@custom_length.register(int)
_(ob): return 1
And on you go for all the types you want to special-case.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20130614/5a2bbbef/attachment.html>
More information about the Python-Dev
mailing list