Sai
Sai

Reputation: 71

What is the meaning of three dots (...) in Python code and where can I see the full body of code?

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: ...

Upvotes: 5

Views: 5179

Answers (2)

Ice
Ice

Reputation: 462

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.

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?

Upvotes: 8

Sy Ker
Sy Ker

Reputation: 2190

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

It's an 'empty' singleton object with no methods, and its interpretation is purely up to context.

  • 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 NumPy slice syntax, it is used 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]]]
    

Upvotes: 11

Related Questions