Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What do parameters in my specific function mean?

Im currently solving an algorithmic task with Python and I got this function, which I should finish:

def solution(nums: Union[List[int], Tuple[int, ...]]) -> int:

I don’t understand what does those thing mean in function. What is nums:? What does nums: Union[List[int], Tuple[int, ...]] mean at all. Could you please describe it? thx in advance

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

nums is the name of the parameter (this is the name that the argument value will be bound to inside your function), and Union[List[int], Tuple[int, ...]] is a type annotation saying that nums is expected to be either a list or a tuple of ints. (A better/simpler way to write this would probably be Sequence[int], although that’s not strictly equivalent, and if the union type was specified as part of the assignment you shouldn’t "correct" your instructor even though in real life you probably wouldn’t specify a type like that.)

The -> int means that the function is expected to return an int.

In other words, the function definition and type annotations are telling you to expect that somebody will call your function with some number of ints, like this:

result = solution([1, 2, 3, 4])

and that you should return an int value, which in the above example would be bound to result.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading