-1

I don't understand the meaning of -> in Python. Searching gives me pages about bitwise operators, but there is no such operator among bitwise operators. It is usually used in a function's description.

Example:

class HelloOperator(BaseOperator):

    def __init__(
            self,
            name: str,
            **kwargs) -> None:
        super().__init__(**kwargs)
        self.name = name
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135

3 Answers3

2

In Python, -> is not an operator.

-> sort of defines what return type a function has. It is called a type hint

Example:

def foo(x) -> int:
  return x + 1

(whereas x should be an int, which you can also specify:

def foo(x: int) -> int:
  return x + 1

)

LeopardShark
  • 3,820
  • 2
  • 19
  • 33
nTerior
  • 159
  • 1
  • 9
1

You have encountered Type Hint, what is behind -> inform what is type of thing returned by function, e.g.

def greeting(name: str) -> str:
    return 'Hello ' + name

means greeting function does return str.

When function or method does not have explicit return it do return None, hence -> None in your example.

Daweo
  • 31,313
  • 3
  • 12
  • 25
0

It's annotation. Programmers use them to show which type will be returned by function.

You can read more about it in the official documentation:

https://docs.python.org/3/howto/annotations.html

UAcapitan
  • 1
  • 1