<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Jun 14, 2013 at 3:12 PM, Martin Schultz <span dir="ltr"><<a href="mailto:maschu09@gmail.com" target="_blank">maschu09@gmail.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr">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.<br>


<br>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.<br>


<br>1. Testing for None:<br><br> 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,...<br>

</div></blockquote><div><br></div><div>Should actually be ``if x is not None``.</div><div> </div><div>[SNIP]</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

<div dir="ltr"><br> - add a good `isiterable` function<br></div></blockquote><div><br></div><div>Done: isinstance(x, collections.abc.Iterable)</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

<div dir="ltr"> - 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<br>

</div></blockquote><div><br></div><div>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.</div>

<div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr">
 - 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.<br>

</div></blockquote><div><br></div><div>The numbers module has a bunch of ABCs you can use to test for integrals, etc. just as I suggested for iterables.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

<div dir="ltr">
 - 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.<br></div></blockquote><div><br></div><div>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.</div>

<div><br></div><div>This is actually a perfect case of using the new single-dispatch generic function work that has landed in Python 3.4 (<a href="http://docs.python.org/3.4/library/functools.html#functools.singledispatch">http://docs.python.org/3.4/library/functools.html#functools.singledispatch</a>). With that you could write your own custom_len() function that dispatches on the type to return exactly what you are after. E.g.::</div>

<div><br></div><div>  @functools.singledispatch</div><div>  def custom_length(ob): return len(ob)</div><div><br></div><div>  @custom_length.register(int)</div><div>  _(ob): return 1</div><div><br></div><div>And on you go for all the types you want to special-case.</div>

</div></div></div>