Python Forum
What do you call this type of parameter?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What do you call this type of parameter?
#1
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?
Reply
#2
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"
Reply
#3
Can I use the type hints to do type checking without mypy? (Or I guess I can actually try it to find out haha.)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  determine parameter type in definition function akbarza 1 580 Aug-24-2023, 01:46 PM
Last Post: deanhystad
  [ERROR] ParamValidationError: Parameter validation failed: Invalid type for parameter gdbengo 3 10,960 Dec-26-2022, 08:48 AM
Last Post: ibreeden
  python call stored procedure with two parameter mg24 4 1,491 Sep-27-2022, 05:02 AM
Last Post: deanhystad
  Why can't I explicitly call __bool__() on sequence type? quazirfan 11 4,635 Aug-20-2021, 06:49 AM
Last Post: Gribouillis
  Type hinting - return type based on parameter micseydel 2 2,468 Jan-14-2020, 01:20 AM
Last Post: micseydel
  what would you call the input for the parameter(s) of a function you have defined? rix 3 2,442 Dec-16-2019, 12:04 AM
Last Post: rix

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020