5 Awful Python Mistakes To Avoid

24,517
0
Published 2024-06-05
In today’s video we’re going to be looking at 5 awful Python mistakes that I’ve made throughout the years. Hopefully this gives you some insight on how to avoid making these mistakes in your own projects!

Master type annotations here:
indently.io/#types

▶ Become job-ready with Python:
www.indently.io/

▶ Follow me on Instagram:
www.instagram.com/indentlyreels

00:00 Learning Python made simple
00:05 Intro
00:26 Bad benchmarks
04:35 Premature optimisations
09:35 Type annotations
15:06 Wrong script
19:12 Planning
21:43 Summing it up

All Comments (21)
  • @falkez1514
    "...post it on stackoverflow, where you can get roasted..." is such a true statement lmfao
  • @TheJaguar1983
    Premature optimisation is a particular issue of mine. Probably more when I'm doing C/C++ code than Python, but I still do it.
  • 11:48 As you mentioned, a vim plugin makes the annotations work :) But even without a plugin, when I read library/someone's code it's much much easier to understand what's going on with type annotations
  • @Finkelfunk
    Who said you don't need to "warm up an interpreter"? Yeah you do. Optimizations and branch prediction of your internal hardware such as the CPU still very much apply, regardless of the language. This however is NOT a clean way to test actual run times. You'd run each test X number of times and then perform statistical analysis, such as using only the 95th percentile in a Gausian Distribution to ensure that outliers are below a certain cutoff point and don't affect the performance.
  • @50mt
    12:28 I definitely didn't mentally autocomplete that sentence with completely the wrong definition of execute...
  • @mikoaj1349
    I must admit I have a problem with overuse of typing in Python. Python is supposed to be a language in which you write code quickly and conveniently. However, annotating just about anything turns it into Pascal or Java 7. Even C++ creators came to their senses and introduced the auto type. If you're going to turn Python into ancient Java, then just use Java in the first place (and preferably its modern version). There's a reason Python is the way it is so why bother with it if you're averse to its basic design principles? Personally I use typing in functions definitions (which I find much more expressive than docs) and inside functions when the interpreter can't infer a type on its own (because hints are nice). However, I'm more than willing to make an exception and NOT use typing if typing were to interfere with Python's dynamic nature, which makes it so versatile and fun to use, for example with SQL queries or some networking applications. Also, typing in Python is just extremely complicated and breaks all the rules which Python was built on, most importantly "easy is better than complicated" and "no exception is important enough to break a rule". Gradual typing would be difficult to implement on its own, but this strange mix of dynamic, gradual and static is unmanageable, especially when introduced into a developed language. There's something seriously wrong if 25% of a Python - an allegedly dynamic language - textbook is dedicated to typing and all those exceptions and exceptions to exceptions. So my advise would be: if you need static typing, use one of the very many statically typed languages. If, however, you're using Python, then use it in the way it was designed for and which makes it great.
  • @joshstickel6554
    The problem with type annotations is that it doesn't work all that fluidly yet: a: int, b: int = (1,2) SyntaxError: invalid syntax so it can hurt your work flow to constantly fight it
  • @DrDeuteron
    just for fun, circa 7:00, you said "sum up until n", which would be interpreted by a native speaker as "sum up to n", your code sums to n-1, which the fix would be to initialize result = n...though that is kinda ugly for those who like ordered math sums, but so is range(n+1) for those who like nice code.
  • @superscatboy
    I can't shake the feeling that if you want high performance and a robust type system, Python might be the wrong tool for the job.
  • Neovim user here. Most people use some sort of language server protocol plugin so we get autocomplete, squiggly lines, etc. the same as vscode. But even when I was just starting Python and didn’t realize you could even DO type annotations, I was putting in comments to remind of type patterns and function signatures. I find it just so much easier to read. Particularly with more complex stuff.
  • @riccardob9026
    About types: I program in Ada which is statically and very strongly typed; it would not cast an integer to a float unless you explicitly ask for it; actually, it would not implicitly convert a port number to, say, a serial number even if they are both integers and.... I just love it! It saves you tons of silly bugs (and let's face it: most bugs are just silly mistakes)
  • @skaruts
    You're getting the main-name check stuff wrong, though. You don't need a "main()" function in your bob script at all, you just need the name check. If you put the tests under a main-name check, then the tests will only be executed if you run bob's script directly. They won't be executed when you run the main script. The name "__main__" is the name python attributes to the scope that is run first -- the entry point of the program. (That name is a key to an internal hash table.) And it's redundant to put that check in the main or in a standalone script that is always going to be the entry point of the program, because that script will always be named __main__, and the check will always return true. You only ever need that check in other scripts, for tests.
  • @xeon566
    Not directly about the topic, but what do you think would be the best way to find these arithmatic options / how to improve at being aware of them? I‘ve had my fair share of maths but i wouldn‘t think about this immediately
  • @joang5023
    Regarding vim editor. There is a plug-in CoC that allows you to configure python auto completion and also warm you when the hint doesn't match with what you wrote
  • @luketurner314
    Types are important even when they are not enforced
  • @Nicoder6884
    5:25 My problem with this code is less to do with performance, and more to do with visuals. No need to waste 4 lines to return a value that's already a boolean and can be returned directly.
  • Eons ago, before we had personal computers and we had to walk to and from school uphill in 20 feet of snow, we had to sketch out the UI (and where necessary a print chart) and then plan the overall program flowchart to link to the UI/print chart before getting approved to make punch cards (yeah, I'm that old). With modern IDEs I find that my students (I teach CompSci at uni) use RAD too often and just don't think things out.
  • my biggest mistake is being a software engineer and not a farmer, I should have listened to the call of the wild but unfortunately, now I am stuck getting tortured by zeros and ones
  • @MeinDeutschkurs
    I’m convinced. Never thought that the advantages are that rich. Thx.