Use of NONE and IS keyword in python || python tutorial #3

preview_player
Показать описание
We can use the None constant in order to represent the absent of a value or in order to represent null. The type of None is NoneType. It isn't possible to create new objects of the NoneType class. We can assign None to multiple variable. All of them will hold the same reference. When converting None into boolean we will get True. None is not equivalent neither to False, 0, empty list, dictionary or string.
When defining a function that doesn't do anything, the type of its returned value will be None. None is also the type of the returned value of functions that their flow returns nothing.
Semantics of None and possible use cases. Difference in the semantics of None in different Python versions
Python adds an implicit "return None" statement to the end of any function. Therefore, if a function doesn't specify a return value it returns None by default.

This means you can replace return None statements with bare return statements or even leave them out completely and still get the same behavior in your programs.

In this video tutorial I show you the rundown of how implicit return statements work in Python and how you can use them to make your code cleaner and more Pythonic.

I also cover the potential downsides of using this feature and how it can make your code *harder* to read in some cases.

On the one hand, you could argue that omitting an explicit return None statement makes the code more concise and therefore easier to read and understand. Subjectively you might also say it makes the code "prettier."

On the other hand, it might surprise some programmers that Python behaves this way. When it comes to writing clean and maintainable code, surprising behavior is rarely a good sign.

Watch the video for the full discussion.
Рекомендации по теме
join shbcf.ru