Why I Explicitly Return “None” In All My Functions (Python)

preview_player
Показать описание
This is why I explicitly return None in all my functions in Python. A lot of us understand that type annotations are good because they allow static type checkers to catch some errors far before we run our code. But another HUGE benefit is that it self documents our code.

▶ Become job-ready with Python:

▶ Follow me on Instagram:

00:00 Returning None
00:26 Reason #1
00:35 Reason #2
01:20 A real example
03:14 Summary
03:47 What do you think?
Рекомендации по теме
Комментарии
Автор

Actually you don't return None explicitly but you just add a type annotation.
Returning it explicitly would mean that you add "return None" at the end of your method :) still good reasoning why you use the type annotation :)

NoName-ktly
Автор

Type annotations have made my life so much easier. I'm often working on so many different projects at once, so coming back to my old python code sometimes feels like I'm using someone else's library, and not my own code that I wrote. Type annotations just help remind me of what exactly every function does.

For me, anyway, I find that if you're planning out your functions but then need to swap projects and come back later, having an explicit "-> None" type annotation helps you remember exactly how you were trying to structure your functions -- no type annotation could mean "I don't want to return anything" OR "I forgot to add type annotations", so having an explicit None return type means "I've thought it through and don't want to return anything".

Silentstrike_
Автор

Your videos are really informative. Love the way you simplify concepts. ❤

deepaksoni
Автор

🎯 Key Takeaways for quick navigation:

00:00 📜 Using type annotations to indicate when a function returns None is a valuable practice.
00:28 📚 Returning None documents that a function is meant to be executed and not return any value.
01:52 🧐 Type annotations help distinguish between functions that return values and those that execute code only.
02:35 💡 Returning None can prevent mistakenly assigning the result of a function that's intended for code execution.
03:16 🚀 Returning None in functions without a return value makes code intent explicit and aids in readability.

Made with HARPA AI

onhazrat
Автор

With python you get all the pain of having to write types without any of the benefits of writing them lol

WilliamReddingGuitar
Автор

I try to consistently have type hints for all arguments and and return types, including “None” return types. I haven’t yet started using type hints with variable just yet but generally the scope is fairly short-lived (inside a small function for example). I’ll get there eventually.

JeremyLangdon
Автор

Very useful consistency. Everyone should use type hints and always return None if the function has no return value.

elatedbento
Автор

It's a crucial part of Python. Thank you. 😊

dipeshsamrawat
Автор

I use type annotation everywhere in Python where it is possible.

It would be nice if you could define somewhere in Python, e.g. at the beginning of a py file, that a type annotation is enforced which then also applies to the return value of functions.

If it is not possible in Python, it would also be nice if a code editor could enforce something like this via an entry in a configuration.

WinfriedKastner
Автор

Very useful one👌These quick tips are great.

krzysiekkrzysiek
Автор

In VSCode (prolly Vim too) the typechecker can infer return type and add it to documentation automatically. So maybe ditch Pycharm instead?

renatoa
Автор

I always add None to functions that end but do not return a value, e.g. a method like sorted.
With Python's weak typing system, it saves code chasing.
I insist on the two developers I manage always putting in type hints unless they would severely limit the function, which has come up exactly twice in a large codebase.

I don't use type hints inside function bodies unless I have a very good reason to do so.

vorpal
Автор

I will use it in future 🔮❤

Informative video❤

devabdul
Автор

I use pyright on strict mode + Precommit before commiting to verify are the types right or not.

shivangrathore
Автор

Why don't you get a warning when you assign the result of a function returning none?
If I do the same in phpstorm it complains.

function a(): void { echo 'A'; }
$a = a();

PugganBacklund
Автор

Lucky that some PL separates function, which returns something and subroutine which doesn't return something.
Nice try on python though.

asagiai
Автор

What editor/IDE is he using? Anyway, good video!

marienkos
Автор

I don't think type annotations are needed, because in vs code if you hover over the function you get the return type and don't really need type annotations

ifeisahildofGod
Автор

My thing is, if you don't explicitly specify a return type, why isn't None implied?

brentjackson
Автор

One of pro OOP rule- never return None

lhardl
join shbcf.ru