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

Python Docstring – two different types of arguments of a function [I'M DESPERATE]

Pros,
I need you right now! I tried to make my function-description look beautiful but it didnt work. The problem is that I have two types of returns: a list and a string. Then you hover on functions vscode shows the returns of the functions like this.
I found out that you can define one type of return using this example (-> str):

def function(x, y) -> str:
  string = "test"
  return string

But let’s say I have tow diffrent return in my code:

def function(x, y):
  if x == 1:
    string = "test"
    return string
  else:
    list = [1, 2]
    return list

How to assign two different types of returns to that function? here is another example

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 :

Depending on your Python version, there are multiple ways this can be epxressed:

  • Prior to 3.9:

    from typing import Union, List
    def foo() -> Union[str, List]: pass 
    
  • Since Python v. 3.9 you can also write:

     from typing import Union
     def foo() -> Union[str, list]: pass
    
  • Since Python v. 3.10 you can also write:

     def foo() -> str | list: pass
    
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