Super Quick Python Refactoring Tips
HTML-код
- Опубликовано: 2 окт 2024
- In this Python Tutorial I show you another 8 quick Python refactoring tips for cleaner and more Pythonic code. This is part 2 of my refactoring code series.
✅ Sourcery - Free VS Code & PyCharm Extension for Refactoring: sourcery.ai/?u... *
Refactoring video part 1: • Quick Python Refactori...
Get my Free NumPy Handbook:
www.python-eng...
⭐ Join Our Discord : / discord
📓 ML Notebooks available on Patreon:
/ patrickloeber
If you enjoyed this video, please subscribe to the channel:
▶️ : / @patloeber
Resources: sourcery.ai/bl...
~~~~~~~~~~~~~~~ CONNECT ~~~~~~~~~~~~~~~
🖥️ Website: www.python-eng...
🐦 Twitter - / patloeber
✉️ Newsletter - www.python-eng...
📸 Instagram - / patloeber
🦾 Discord: / discord
▶️ Subscribe: / @patloeber
~~~~~~~~~~~~~~ SUPPORT ME ~~~~~~~~~~~~~~
🅿 Patreon - / patrickloeber
#Python
----------------------------------------------------------------------------------------------------------
This is an affiliate link. By clicking on it you will not have any additional costs, instead you will support me and my project. Thank you so much for the support! 🙏
Small correction: In Tip 7 it should be - if currency in {"USD", "EUR"} - to use a set. So it must be curly braces and not parenthesis. Hope you enjoyed the video!
I use this all the time with a List. Will change it to a Set moving forward. Excellent video as always!
Isn't a tuple for effective for this. A set would only be useful when you want to enforce uniqueness. In this case a tuple is perfect.
I wonder what’s the performance difference in such a small collection between list set and tuple
In all earnest... There's no difference whether you use a list, a set,or a tuple here. There are just not enough elements to feel any difference. It'll start to show only when the number of elements goes into, say, hundreds or thousands.
you can also predefine standard_currencies = {"USD", "EUR"} outside the function to avoid the creation of the set each time the function is called
4:00: values are in a tuple/() not in a set/{}. Did you mean tuple or set actually?
You must have gotten the answer already, but I am just putting it here for the comments. It should be a set like he said, not a tuple like was shown.
so the correct line is --> if currency in {"USD", "EUR"}: for a set but
Probably both (line shown) work as well. Just remember set has unique items, tuples does not (have to)
4:00 Isn't a set in Python initialized with curly braces?
Totally right, thanks for letting me know! I pinned a comment with the correction
2:00 don’t rely on enumerate’s counter variable outside the loop, because an empty argument will lead to a NameError, as the counter assignment is never executed. I have been burned by this in the else block.
tip number 1 is not even tip. 🧐 that's how people declared list.
had to start with an easy one :D but sometimes you still find this in other people's code...
tabnine + sourcery = excellence =D
Thank you for this tips :)
Hi. Nice tricks
I was trying 03:52 before I watch it :D
currencies = ["USD", "EUR"]
def process_payment(payment, currency):
if currency in currencies:
print(f"Payment: {payment} and currency is: {currency}")
else:
print(f"Global: {payment}")
Thanks ^_^
nice!
Nice tips, thanks. could you please do some python code implementation for the Passive-aggressive algorithm for the multiclass classification case?
thanks! Will have a look at that
where's tip no.0?
index not starting with 0?
For tip #7, I would do like this so I don't need if's:
payment_types = {"USD": process_standard_payment, "EUR": process_international_payment}
def process_payment(payment, currency):
payment_types[currency](payment)
that's not the same code.
video's code was
"run standard_payment if currency is "CSD" or "EUR", otherwise run international one"
but your code is "run different payment depend on the currency"
"We should be always searching for opportunities to remove duplicated code" - now I know you're like me :) thanks for the tips!
Except “is_winning” is undefined.. so assuming most people would try these in repl and find them not to work, why not come out with better example?
amazing video...! please do more videos like that with refactore code, "code smell" I learn a a lot from that. and BTW the AI tool is nice... I you know more like this please let us know (:
Dam, That's good. Look forward for more such videos
can you make subtitles in german.
Which VS code theme don you use??
4:02 you show tuple instead of set.
Yeah noticed this after the video :( thanks for the hint, I already pinned a comment
I know this except 5 and 8 points.
Great! At least you learned 2 new things :)
@@patloeber humm Thanks
Thanks for the video, it was useful
Nice video bro..🔥🔥🤙
Fascinating 😝😝😝 💖🔆🤪
great video
Thanks!
Tip 7. When checking if character is in string, should I convert a string into set?
Unless you’re checking a lot of characters, there’s no point. Turning a string into a set takes the same amount of time as just checking if a character exists in a string.
@@MrDgf97 thanks!
Tip number 1 xD? For those people that cant tie their shoes.
Gotta start with a simple one 😂
Thank you, some great tips here
Glad it was helpful!