[Python-ideas] Allow "assigning" to Ellipse to discard values.

Spencer Brown spencerb21 at live.com
Tue Feb 10 22:01:12 CET 2015


Basically, in any location where a variable name is provided for assignment (tuple unpacking, for loops, function parameters, etc) a variable name can be replaced by '...' to indicate that that value will not be used. This would be syntactically equivalent to del-ing the variable immediately after, except that the assignment never needs to be done.

Examples:
>>> tup =  ('val1', 'val2', (123, 456, 789))
>>> name, ..., (..., value, ...) = tup
instead of:
>>> name = tup[0]
>>> value = tup[2][1]
This shows slightly more clearly what format the tuple will be in, and provides some additional error-checking - the unpack will fail if the tuple is a different size or format.

>>> for ... in range(100):
>>>    pass
This particular case could be optimised by checking to see if the iterator has a len() method, and if so directly looping that many times in C instead of executing the __next__() method repeatedly.

>>> for i, ... in enumerate(object):
is equivalent to:
>>> for i in range(len(object)): 
The Ellipse form is slightly more clear and works for objects without len(). 

>>> first, second, *... = iterator
In the normal case, this would extract the first two values, and exhaust the rest of the iterator. Perhaps this could be special-cased so the iterator is left alone, and the 3rd+ values are still available.

>>> def function(param1, ..., param3, key1=True, key2=False, **...):
>>>     pass
This could be useful when overriding functions in a subclass, or writing callback functions. Here we don't actually need the 2nd argument. The '**...' would be the only allowable form of Ellipse in keyword arguments, and just allows any other keywords to be given to the function.

- Spencer


More information about the Python-ideas mailing list