What does it mean if an argument has a colon when defining a function?

Asked 1 years ago, Updated 1 years ago, 92 views

I'm playing with checkiO and the function presented is

It also says def data (password: str):

It also says def data (password: str) -> str:

I'm just a beginner at Python, so I only know that you specify variable values like this.Is this far?

python3.7 def function arguments

2022-09-22 20:20

1 Answers

It's called a type hint.

Python is a language in which types are determined dynamically. That means you don't know what type it is until you do it.

This means, in other words, that an error by type cannot be known unless it is runtime (running).

def hab(arg1, arg2):
    return arg1 /arg2 

When a simple division function exists, as shown in

Calling with hab(10,2) will work as expected, but... Calling hab ("aaaa", "bbbb") will result in an error.

At this time, you can write code that can be explicitly typed as below to prevent errors and is easy to maintain.

def hab(arg1, arg2: int) -> int:
    return arg1 /arg2

One more thing is that the performance of hab ("aaaa", "bbbb") is not preventive because it is not compulsory. It's just a hint.


2022-09-22 20:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.