What is the syntax of "..." (ellipsis)?

I looked at the source code of the Blender add-in, and I saw the new syntax:

def elem_name_ensure_class(elem, clss=...): elem_name, elem_class = elem_split_name_class(elem) if clss is not ...: assert(elem_class == clss) return elem_name.decode('utf-8') 

What is the point ... ?

+5
source share
1 answer

... is the literal syntax for the Python Elipsis object :

 >>> ... Ellipsis 

Mostly used by NumPy; see What does the Python Ellipsis object do?

The code you found uses it as a sentinel; a way to detect that no other value was given for the clss keyword argument. Usually you use None for this value, but this will disqualify None itself from being used as the value.

Personally, I do not like to use Ellipsis as a Ellipsis ; Instead, I always created a dedicated watchdog:

 _sentinel = object() def elem_name_ensure_class(elem, clss=_sentinel): elem_name, elem_class = elem_split_name_class(elem) if clss is not _sentinel: assert(elem_class == clss) return elem_name.decode('utf-8') 

Using the notation ... outside the subscription ( object[...] ) is a syntax error in Python 2, so the trick used restricts Python 3 code.

+13
source

All Articles