[Python-Dev] Clean way in python to test for None, empty, scalar, and list/ndarray? A prayer to the gods of Python (original) (raw)
Tres Seaver tseaver at palladion.com
Sat Jun 15 23:45:33 CEST 2013
- Previous message: [Python-Dev] Clean way in python to test for None, empty, scalar, and list/ndarray? A prayer to the gods of Python
- Next message: [Python-Dev] Clean way in python to test for None, empty, scalar, and list/ndarray? A prayer to the gods of Python
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On 06/15/2013 04:11 PM, Terry Reedy wrote:
On 6/15/2013 8:53 AM, Tres Seaver wrote:
In fifteen years of Python programming, I have literally never wanted to iterate over 'str' (or now 'bytes'). If so, it is because you have always been able to use pre-written methods and functions that internally do the iteration for you.
Given that strings are implemented in C, there is no real "iteration" happing (in the Python sense) in their methods. What stdlib code can you point to that does iterate over them in Python? I.e.:
for c in stringval: ...
Even if there were, aren't you proving my point? The fact that strings are implicitly iterable injects all kinds of fur into methods which take either a single value or an iterable as an argument, e.g.:
def api_function(value_or_values): if isinstance(value_or_values, STRING_TYPES): value_or_values = [value_or_values] elif isinstance(value_or_values, collections.abc.Iterable): value_or_values = list(value_or_values) else: value_or_values = [value_or_values]
The bugs that leaving the first test out yields pop up all over the place.
I've always considered the fact
that Python made them iterable by default (rather than e.g. defining a method / property to get to an iterable "view" on the underlying string) .iter is that method.
But it is on be default: I was describing something which has to be requested in ways that don't get triggered by syntax, and doesn't make strings look "non-scalar" for APIs like the one above.
Tres. - --
Tres Seaver +1 540-429-0999 tseaver at palladion.com Palladion Software "Excellence by Design" http://palladion.com -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) Comment: Using GnuPG with undefined - http://www.enigmail.net/
iEYEARECAAYFAlG84H0ACgkQ+gerLs4ltQ56RQCgks8R52f41RwJ+v9oteOBC3qY kIIAoIHmg+zcmJpV3v/gAhkKJfbNKufj =ZeRB -----END PGP SIGNATURE-----
- Previous message: [Python-Dev] Clean way in python to test for None, empty, scalar, and list/ndarray? A prayer to the gods of Python
- Next message: [Python-Dev] Clean way in python to test for None, empty, scalar, and list/ndarray? A prayer to the gods of Python
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]