Python Forum

Full Version: What do you call this type of parameter?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
In some code I've seen stuff like this:

def some_function(a -> int):
    #do something
I thought a -> int was called a decorator, but a decorator seems to be this:

def some_function(func):
    def some_func(*args):
        #do something to func
        return func(*args)
    return some_func

@some_function
def some_other_function(params):
    #do something
So if a -> int is not a decorator, what's it called and what does it do?
It's type hints added in Python 3.5.
So it's hints and not forced yet maybe in 3.7.

So if run in Python 3.6 no error massage is generated,
have to use 3-party library like mypy to get a error message about wrong types.
Example:
hello.py
def greeting(name: str) -> str:
    return f'Hello {name}'

print(greeting('Kent'))
hello_int.py
def greeting(name: str) -> str:
    return f'Hello {name}'

print(greeting(100))
With mypy pip install mypy
E:\1
λ mypy hello.py
# No error

E:\1
λ mypy hello_int.py
hello_int.py:4: error: Argument 1 to "greeting" has incompatible type "int"; expected "str"
Can I use the type hints to do type checking without mypy? (Or I guess I can actually try it to find out haha.)