5 Python BAD Practices To Avoid

preview_player
Показать описание
Welcome back to another video! In this video, I'll be covering five bad python practices you should be avoiding. Most of these are things that seem normal or natural for people to do, however, they can lead to problems later and can make code look messy and unreadable.

⭐️ Timestamps ⭐️
00:00 | Overview
01:47 | Mutable Default Parameters
06:24 | Not Using A Default Dict
11:27 | Not Using A Context Manager
14:54 | Not Using Enumerate
17:19 | Overriding Built-In Keywords

◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️
💰 Courses & Merch 💰

🔗 Social Medias 🔗

🎬 My YouTube Gear 🎬

💸 Donations 💸
◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️

⭐️ Tags ⭐️
- Tech With Tim
- Python
- Bad Practices
- Messy Code
- Python Programming
- Vidyard

⭐️ Hashtags ⭐️
#TechWithTim #Python
Рекомендации по теме
Комментарии
Автор

Hey man, just finished my first website thanks to your flask tutorial. Thank you for taking your time and experience and sharing it with us so that we can learn!

mateoortega
Автор

Hello Tim.
Wow, 🤩 great content as always, thanks for sharing it!🙏🏻
I just loved it💜!

DevMadeEasy
Автор

Using a default dict in the mentioned case is not really pythonic either, since there is a counter in the collections module. The code would then become:

from collections import Counter
numbers = [ ... ]
c = Counter(numbers)

the Counter API offers a lot of additional functionality and is generally to be preferred over the default dict. The default dict should be used when there is some additional work to do on insertion, like it was done in the second case.


Regarding the context manager: If you catch yourself writing many try–finally blocks, that is normally the moment to use one. You can also implement custom managers for your own classes via the dunder methods `__enter__` and `__exit__`. Those are what is executed by the with statement:

with Foo(args) as bar:
baz()

more or less desugars to

bar = Foo.__enter__(args)
try:
baz()
finally:
Foo.__exit__()

JaycenGiga
Автор

Excellent video. We often learn about what is a "pythonic" way of doing something without an explanation on the reasons behind it, is it a slight difference in performance like with defaultdict and enum, or can bad and unexpected things happen.

Theorose
Автор

knew a good pprtion of these but now its time to go refactor some code for those mutable default params... whoops.
need more of these videos! great job!

moo_goo
Автор

Tim literally drops a video everyday... Bless Up Man!!!

kunle
Автор

good beginner tips, couple comments:

- the mutable args is not a bad practice, it's a bug because the code won't work as intended, the usual way to deal with that is to default to None and then check to assign the mutable object

- the defaultdict is a good tip but if you're counting there's an even more specialized object class Counter that you can use

- `with open()` works because open() can be used in that fashion but not all classes do, there's a helper called closing() in the contextlib module that helps with that cases

SamusUy
Автор

6:25 I think that is better to use the class Counter (from collections as well), which does the exact same as defaultdict, but you don't have to do any for loop.

>> form collections import Counter
>> counter = Counter([1, 2, 3, 4, 1, 4, 4, 1, 4, 3, 3, 2])
>> counter
Counter({4: 4, 1: 3, 3: 3, 2: 2})
>> conter = dict(counter)
>> counter
{1: 3, 2: 2, 3: 3, 4: 4}

Nice video

gustavoalebranchirod
Автор

Great vid, had no idea about setdefault, it's basically a defaultdict when you can't use imported modules.
Thanks

decoybuilder
Автор

For the number count one you can also use:
counts = dict.fromkeys(set(numbers), 0)
for key in numbers:
counts[key] += 1
(although this requires the numbers to be created before the dict, and its not so easily updatable, but still an ok method)

csabamolnar
Автор

Hi Tim! What keyboard do you use? Are you happy with it or would you recommend something else for programming? Thanks!

willsee
Автор

very insightful!! thank you @TechWithTim

markmadhukar
Автор

Alright someone is thinking about intermediate developers! Thanks Tim, these are really helpful!

sarojkarki
Автор

In the first example I think of the parameters being passed 'by reference' versus 'by value'. Whats interesting is that the mutable parameter has a lifetime that is not limited to a single call of the function which we would expect for a local variable in the function. Instead the mutable parameter stays in memory and is acting like a static class variable. We could print the memory location of the mutable parameter to see that this is the case.

marksegall
Автор

17:25 don't override "keyboards"
OKAY

goranorsolic
Автор

Hi Tim. I love your videos. They have helped me endlessly. But one thing I'd like to call out, is that you use names that are too similar to built-in words, such as "list", "string", and "dict" which may make things very confusing to people who are new to programming (e.g. "Why does he say 'string +='??").

I'd suggest writing things like "the_list", "new_string", or "my_dict" so that people are more likely to understand that these are custom variables, arguments/parameters, or objects, rather than built-in words that may throw errors when they try to write a program that uses things like "str", "list", and "dict" (etc).

Thanks again and I hope you have a great weekend!

Zancb
Автор

just an Amazing video for ppl who are getting started with python!

natiqueibrar
Автор

6:24 for that I might just as well write
counts = {key: numbers.count(key) for key in set(numbers)}

not_vinkami
Автор

Hello could I code with a 16gb ram, i5 10th gen and a nvida 3060 ?

maxburton
Автор

Could you give me an advice? Because I don't know what I can do. I'm immediately advanced and I want to make a big project but I can't. It's just too hard. And now I don't know how to get to the higher level. How I can improve my skills without watching next tutorial?
Should I practise more? Could you help and recommend something? Thanks.

frdu