This is a far more modest alternative to
https://mail.python.org/archives/list/[email protected]/thread/ZP2OKYEH5ZQLNJDCWVF3X6AEB3VQQT6V/
and is also something that could be reasonably & easily implemented in 3.x.
Add `Scalar` to `collections.abc`. The meaning of `Scalar` is that the object
is generally to be treated as an atomic value, regardless of whether it is also
technically a container. The obvious application of an object being both a
`Container` and a `Scalar` is to stringlike objects, but there might also be
other kinds of standard object for which it would be useful and that I have not
thought of.
One reason for wanting to designate strings as `Scalar` is for navigating a
hierarchy of collections in which, even though a string is a collection, we
want to treat it as a leaf of the tree rather than drilling down into its
characters (each of which is represented as a string of 1 character —
`type('xyz'[1])` -> `str`)
Example implementation (for addition to _collections_abc.py):
class Scalar(metaclass=ABCMeta):
__slots__ = ()
@classmethod
def __subclasshook__(cls, C):
if cls is Scalar:
if not issubclass(C, Collection):
return True
return NotImplemented
Scalar.register(str)
Scalar.register(bytes)
Scalar.register(bytearray)
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/OVP6SIOFNGGENJAJHXOS2AEUUPWSSRD2/
Code of Conduct: http://python.org/psf/codeofconduct/