Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
closed by

In python 3.x, it is entirely expected to utilize return type explanation of a capacity/function, for example, 

def foo() -> str:

    return "bar"

What is the right explanation for the "void" type?

I'm considering these three options:

def foo() -> None:

  • not logical IMO, because None is not a type,

def foo() -> type(None):

  • using the best syntax I know for obtaining NoneType,

def foo():

  • omit explicit return type information.

Choice 2. appears to be the most consistent to me, however, I've effectively seen a few occurrences of 1.

        

closed

4 Answers

0 votes
by (25.7k points)
selected by
 
Best answer
In Python 3.x, the correct annotation to indicate a function with no return value (similar to the "void" type in some other programming languages) is None. However, the second option you mentioned, def foo() -> type(None):, is the recommended way to annotate such functions.

Here's the preferred annotation for a function with no return value:

def foo() -> None:

    # Function code

The None type represents the absence of a value in Python, and it serves as a special value to indicate no return value. While it's true that None is not a type in the strictest sense, it is commonly used in this context to indicate that the function does not return anything.

Using def foo(): without explicit return type information is also valid and will work as expected. However, explicitly indicating None as the return type provides additional clarity to both human readers and static type checkers. It makes it explicit that the function does not return any meaningful value.

Ultimately, the choice between options 1 and 2 is a matter of personal preference and style guidelines. Some developers may prefer to be explicit and use def foo() -> None, while others may choose to omit the return type annotation for simplicity. Both options are acceptable in Python.
0 votes
by (26.4k points)

This is directly from PEP 484 - Type Hints documentation: 

When used in a type hint, the expression None is considered equivalent to type(None).

Also, as you can see the majority of the models utilize None as bring type back.     

Are you interested to learn the concepts of Python? Join the python training course fast!

Watch this video tutorial for more information

0 votes
by (15.4k points)

In Python 3.x, when annotating a function to indicate it has no return value (similar to the "void" type in other programming languages), the recommended approach is to use None as the return type. Therefore, the correct annotation for a function with no return value is def foo() -> None:.

While None is not technically a type in the strictest sense, it serves as a special value in Python to represent the absence of a value. By explicitly indicating None as the return type, you make it clear that the function does not return any meaningful value.

Alternatively, you can choose to omit the return type information altogether by using def foo():. This is a valid option, but explicitly specifying None provides additional clarity for both human readers and static type checkers.

Ultimately, the decision between these options depends on personal preference and style guidelines. Some developers prefer the explicit def foo() -> None annotation, while others opt for simplicity and omit the return type annotation. Both approaches are acceptable in Python.

0 votes
by (19k points)

When it comes to annotating functions in Python 3.x to indicate they have no return value (similar to the "void" type in other programming languages), the recommended approach is to use None as the return type. This means that the appropriate annotation for a function with no return value would be def foo() -> None:.

Although None isn't strictly considered a type, it serves as a special value in Python to represent the absence of a meaningful return value. By explicitly specifying None as the return type, you make it clear that the function doesn't yield any useful result.

Alternatively, you have the option to omit the return type information by using def foo():. While this approach is valid, providing the explicit None return type offers enhanced clarity for human readers and static type checkers.

Ultimately, the choice between these options relies on personal preference and the style guidelines you follow. Some developers prefer the explicit def foo() -> None annotation, while others opt for simplicity and omit the return type annotation altogether. Both approaches are considered acceptable in Python.

Related questions

0 votes
1 answer
asked Nov 19, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
asked Nov 12, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
0 votes
1 answer
asked Aug 6, 2019 in Java by Krishna (2.6k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...