5 Good Python Habits

preview_player
Показать описание
Here are 5 good habits you should consider building in Python.

▶ Become job-ready with Python:

▶ Follow me on Instagram:

00:00 Learning Python made simple
00:05 if __name__ == ‘__main__’
03:00 main()
04:50 Big functions
08:07 Type Annotations
14:39 List comprehensions
17:22 Outro
Рекомендации по теме
Комментарии
Автор

A huge thanks to you guys who are sharing more useful habits and pointing out some of the benefits that I left out in the comment section. You help both me and many other developers to understand more about this beautiful programming world! 

I'm super appreciative when you guys share cool information :)

Indently
Автор

after hour and hours and trainings, someone explains in 3 seconds the role of if __name__ = ''__main__', omg, thx a lot !

alcsutzul
Автор

Another important thing with main() is the scope. Code run in "if name main" uses global scope and can/will pollute it. Running code in main has the function's scope :)

DamienDegois
Автор

Hats off to my main man Bob, he did nothing wrong.

PikalaxALT
Автор

A main(with parameters) is even more useful when testing. Allow the if __name__ block to handle command line arguments and then pass sanitized versions of those to your main. Then your tests can just call main directly and test various scenarios.

Aplysia
Автор

Just one thing about list comprehensions: they can very easily become very hard to read, so I'd say "don't overuse them"

farzadmf
Автор

At 10:45 it is better to use Iterable from the typing module to annotate "elements". This way you could pass not only lists but also tuples or even generators.

artyom
Автор

For number 2, another bonus is if you declare a main function you can expose it in an init or import it. One example is importing when testing.

AnthonyFammartino
Автор

Slight tweak to this main() thing. I think its better to have main return an int and then have sys.exit(main()) in the if name == main bit. That way the program behaves like a normal commandline program with an exit status. You can add in error handling and return a non-zero exit status easily this way.

ejames
Автор

4:11 thanks for this. Structuring my code to have a main function that runs the others and an if statement that runs main is a great idea to clearly show what the program does.

justwatching
Автор

for list comprehensions: a lot of common patterns can be covered by `filter` and `map`. For example, instead of doing [p for p in people if len(p) > 7] you can do filter(lambda p: len(p) > 7, people) which I find quite a lot more readable because it's explicitly telling you that the only thing happening in this statement is we're getting rid of some values based on a predicate.
Although, as a counterpoint, python's lambdas and higher order functions like `filter` and `map` can be somewhat verbose so the choice is not so straightforward.

KingJellyfishII
Автор

Super appreciate this. Need to include type hinting into my coding. Your explanations make sense.

mathewrtaylor
Автор

This came at PRECISELY the right time for the type annotations stuff 🎉 big thanks

damien__j
Автор

I think the best reason to make a main function is the fact you can push utility functions to the bottom of the file and leave the *main* code at the top, not needing to scroll down to see the what the actual main code is.

AmodeusR
Автор

Another great video with great examples. Thank you. Mypy, who knew?

michaelrstudley
Автор

In the case of making [p for p in people if len(p) > 7] more understandable inherently, I would opt for naming, such as 'n', 'name, ' 'name_str' .. [name for name in people if len(name) > 7]. Great vid!

GereBear
Автор

I've been following you for few months now, you provide amazing content, and I really appreciate your videos. Thank you so much. You are helping so many students😊

alexlim
Автор

I closed the video on "waste time on writing obvious documentation"

fernandino
Автор

For the first two tips i would encourage to write real tests instead of "tests inside a module". Yes, it will take extra time, but will serve much better.

uuuummm
Автор

I’m glad! Your videos become my scripts raise to another level

mellolta