5

This is the code of the atan and tan functions in the math module in Python. Their body consist of three dots (...). Many built-in functions also do the same. What is the meaning of that and where can I see the full body instead of three dots?

def tan(__x: _SupportsFloatOrIndex) ->float: ...

def atan(__x: _SupportsFloatOrIndex) ->float: ...
Tonechas
  • 13,398
  • 16
  • 46
  • 80
Sai
  • 71
  • 7
  • Does this answer your question? [Where are math.py and sys.py?](https://stackoverflow.com/questions/18857355/where-are-math-py-and-sys-py) – Jan Wilamowski Jan 06 '22 at 06:12

2 Answers2

6

It's the Ellipsis, it has many meanings and none at all. Let me explain;

It's an 'empty' singleton object with no method and its interpretation is purely up to whatever implements the __getitem__ function and sees Ellipsis objects there.

  • It can be used as a substitute for pass or not yet implemented code;
def my_function(arg): 
    ...
  • It is used to denote certain types to a static type checker when using the typing module (for type hints).
def partial(func: Callable[..., str], *args) -> Callable[..., str]:
    # code
  • In slice syntax to represent the full slice in remaining dimensions;
import numpy as np
>>>   
>>> array = np.random.rand(2, 2, 2, 2)
>>> print(array[..., 0])

[[[0.03265588 0.85912865]
  [0.45491733 0.3654667 ]]

 [[0.58577745 0.11642329]
  [0.88552571 0.69755581]]]

Thank you so much for this question, I learned something new in Python today.

Sy Ker
  • 2,047
  • 1
  • 4
  • 20
6

It seems you dug into the math library.

Actually, the file you are referencing is a Python module written in C. In order to add type-hints to that file (which is an "Extension Module" as it is written in C), the type hints are added to a "stub" file with the extension .pyi.

Type-hints are added via a stub-file *.pyi.

Here the ellipsis (...) is part of the syntax, so the code-block below really shows the complete file contents.

Here is a detailed explanation: What is the use of stub files (.pyi ) in python?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Ice Griffin
  • 430
  • 1
  • 10