length of a tuple or a list containing only one element

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Mon Nov 3 08:45:52 EST 2008


TP:
> This is actually the length of a bracketed string, not a tuple.
> Tuple's are defined by the existence of a comma...try:
> >>> len(('foo',))
> 1

Time ago I have suggested to change the tuple literal, to avoid the
warts of the singleton and empty tuple, that may lead to bugs. But
using ASCII alone it's not easy to find something suitable and nice.

One of the suggestions of mine was to use (|...|) or [| x, ...  |] to
denote tuples. This may lead to problems with the bitwise operators,
so the following two tuples:
t1 = ()
t2 = (x | y,)

Become (Fortress language uses similar liters, I think):
t1 = (||)
t2 = (| x OR y |)

Or this:
t1 = [||]
t2 = [| x OR y |]

Where OR, AND, XOR, NOT, SHL, SHR are the bitwise operators.
Having to type (| |) often is less handy, for example this code:

def divmod(a, b):
    return a // b, a % b
d, r = divmod(10, 7)

becomes:

def divmod(a, b):
    return (| a // b, a % b |)
(|d, r|) = divmod(10, 7)

Bye,
bearophile



More information about the Python-list mailing list