I came across function with signature like this:

def get_quantile(numbers: List[float], q: float | int ) -> float | int | None :

What does it mean?

It's a syntax error on my python 3.8. Do I need to import something from future to make it work?

InSync's user avatar

InSync

12.3k5 gold badges23 silver badges61 bronze badges

asked Oct 30, 2020 at 21:25

Kroshka Kartoshka's user avatar

1

According to PEP 604, | will be used to designate union types from Python 3.10.

So float | int will mean Union[float, int], i.e. a float or an int.

answered Oct 30, 2020 at 21:30

khelwood's user avatar

Sign up to request clarification or add additional context in comments.

It means or. So q: float | int means that q may either be a float or an int.

answered Oct 30, 2020 at 21:30

iHowell's user avatar

2 Comments

Are you sure or can be in an annotation? Apart from some wierd calculations of types and values. Sorry for attacking your answer, but you made "or" look like a Python keyword which might mislead somebody ;)

2020-10-30T21:32:17.017Z+00:00

It doesn't really mean or, but more properly "union", which is consistent with the usage of this same operator for sets and dictionaries. And no, the or keyword can't be used in the type annotation.

2022-06-09T16:44:08.993Z+00:00

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.