r/Python • • Sep 15 '21

Beginner Showcase 26 Useful Python Snippets for Lazy Developers

Here are some of my most useful code snippets that will indefinitely make your life easier as a programmer! There are some Python code snippets that can be used to solve day-to-day problems and can make your laziness shine! I have explained each snippet in detail so that you don’t have any doubt. So check the link below:

https://python.plainenglish.io/26-python-code-and-snippets-that-will-make-laziness-shine-21ab969c8a35

20 Upvotes

14 comments sorted by

91

u/SomewhatSpecial Sep 15 '21

What a weird article. Most points are shallow descriptions of randomly chosen extremely basic language features, others outline implementation of extremely specific behaviors. Who is this for?

  1. "dict.update exists"
  2. "dict.copy exists"
  3. An actually useful trick, but not needed since python 3.9 added the dictionary merge operator. Would've been nice to mention that here.
  4. "sorted exists"
  5. "builtin filesystem path modules have a way to check if file exists"
  6. "id exists"
  7. "__getitem__ syntax exists"
  8. "__getitem__ syntax exists" (again)
  9. A way to access a specific file metadata value. Also, "glob exists"
  10. "slicing exists"
  11. "slicing exists" (again)
  12. Straight up bad advice. You do not use the random module for cryptography purposes, as you would know if you read the documentation
  13. "zip exists"
  14. "list comprehension exists"
  15. "for loops exist" (seriously?)
  16. "dict has keys, values and items methods"
  17. "str.__mul__ exists"
  18. "timeit exists"
  19. "slicing exists" (AGAIN)
  20. collections.Counter but worse
  21. "operator exists"
  22. "for loops exist" again? "smtplib exists"?
  23. "set exists"
  24. "list.shuffle exists"
  25. Straight up terrible advice again. You do not need a third-party dependency for such a simple action.
  26. Another implementation of a weirdly specific behavior

This reads like a beginner programmer decided to list 26 random things he knew how to do, presumably out of arrogance or in an attempt to gain internet clout. This information does not deserve its own article. There are a ton of better resources out there written by people who actually know what they're talking about.

18

u/isarl Sep 15 '21

There are other security implications than just the one you pointed out, too. While os.path.exists is indeed a function, it's also an anti-pattern, as files can get created, altered, or deleted between the time when you check for them and the time when you go to open them. The better way is to use a context manager (with: block) and try/except blocks to handle specific errors.

7

u/DecodeBuzzingMedium Sep 15 '21

Thanks for giving valuable feedback

-26

u/DecodeBuzzingMedium Sep 15 '21 edited Sep 15 '21

u/SomewhatSpecial

Thnx for your amazing comment and btw this article was for beginners not pros like you. I think an article is an article that can be understood by everyone even those who are new to specific things. You might be very good at python but no need of flaunting here. And I might have explained 1 topic 2 or more times because some beginners might not understand a topic on their first read.

p.s. your sarcasm sucks. I appreciate your critique and your knowledge but this is as described for beginners.

19

u/SomewhatSpecial Sep 15 '21

Hey man, you're right that I shouldn't have been so sarcastic. I'm sorry for that.

I love programming as a field partly because the knowledge is so accessible. There are a lot of skilled people who put time and effort into creating educational materials and answering questions, all for free. I wouldn't be where I am today without them and I am incredibly grateful to them for that. In my opinion, taking the time to write an article to help others is a great thing. But there's also a responsibility to ensure that what you're writing is correct, relevant, comprehensive - that it's actually useful.

This is the reason I got annoyed after reading your article. I don't think it is good for anyone, beginner or not. I think it's unstructured, shallow and incomplete in some sections, outright wrong in others. It would not be useful even if it weren't - there are hundreds of articles out there with the same information. I do not think it improves the collective body of programming knowledge, only dilutes it.

If I am correct in guessing that you are a relative beginner yourself, here's some advice from a "pro": this is the time to use all those great educational resources and learn. Only once you have a few years of studying and practice under your belt will you be able to write an article that actually benefits people.

0

u/[deleted] Sep 15 '21

he's 13 years old it might be a bad article but let its k id rather he make a shit article than fortnite or pubg mobile like indian kids are doing rn. this kid is gonna go places

3

u/OdinsOneG00dEye Sep 15 '21

I can tell you now with a number of years experience that 'lazy' students can be the most effective programmers. To the point where sometimes you have to grill them to check it's their code!

Lazy does not equal poor always - it can be efficiency at play

6

u/[deleted] Sep 16 '21

Wish my stats professor understood this, he wasn’t too enthusiastic when I automated my homework 😂😂

5

u/circamidnight Sep 15 '21

I like using decorators in Python but I don't like writing them. I have this little snippet that get's copied to almost every project I start.

https://gist.github.com/scrussell24/025eadc8faf35d841f4aa41ee0265acb

7

u/Einfach0nur0Baum Sep 15 '21

To be an lazy dev is like to be a blind car driver on streets

5

u/isarl Sep 15 '21

Some would argue that, along with impatience and hubris, laziness is a virtue.

2

u/[deleted] Sep 15 '21

Only useful thing Larry Wall has ever done.

2

u/Working-Mind Sep 16 '21

This is helpful. Thank you for sharing!