What is ... meaning in python

Can someone explain to me what the … means here?
like this: days: float = ...,
or : def dst(self) -> timedelta | None: ...

... is the Ellipsis literal.

It’s a just a value that usually does nothing, so can be used as a quick easy to type placeholder for “uninitialized” variables or “unimplemented” functions or in examples for “something”. It is a valid syntax so you can use it if you want to avoid a syntax error for now but don’t know yet what to put there. But usually you would probably not leave it like that in production.

(It also has a special meaning in slicing syntax, meaning “any additional dimension”.)

1 Like

In this particular case, these definitions would be type stubs, used to define the types for functions, classes etc when the original code does not contain types. Since for that use case the type checker doesn’t need to know how the function is implemented, ... is used in the body to prevent a syntax error. Same for the default argument, all it needs to know is that there is one.

3 Likes

In general, ... (also spelled as Ellipsis) is a special value. It’s a lot like None: you can’t make more instances of the same type, and it doesn’t provide any particularly useful methods. Unlike None, it isn’t automatically returned from functions, and it’s used for different purposes. Its meaning comes from the context where it’s used. In the context that you show, it’s most likely as @TeamSpen210 explained. In Numpy, it’s used for Numpy’s special slicing syntax, to mean “all the elements, along as many dimensions as possible in this spot”. For example:

>>> import numpy as np
>>> arr = np.array([[[1,2],[3,4]],[[5,6],[7,8]]])
>>> arr[:,1]
array([[3, 4],
       [7, 8]])
>>> arr[...,1] # same as arr[:,:,1]
array([[2, 4],
       [6, 8]])

It’s also used in example code instead of pass, to make it clear that there’s more code that’s being omitted (while still using legal Python syntax):

# This probably means a function that does something,
# but I'm not showing you what it does because it's irrelevant
def example1():
    ...

# This probably means a function that does nothing
def example2():
    pass
1 Like

Or sometimes it means “and I haven’t written this part yet”. I use this when stubbing code out; “pass” means “this does nothing, and that’s correct”, while “…” means “lemme get back to you on this”.

It’s a handy bit of syntax.

2 Likes