Тёмный

Quick Python Refactoring Tips 

Patrick Loeber
Подписаться 273 тыс.
Просмотров 113 тыс.
50% 1

In this Python Tutorial I show you 8 quick Python refactoring tips for cleaner and more Pythonic code.
✅ Sourcery - Free VS Code & PyCharm Extension for Refactoring: sourcery.ai/?u... *
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! 🙏

Опубликовано:

 

26 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 83   
@patloeber
@patloeber 3 года назад
I hope you like those tips! If you want to install the Refactoring Extension in VS Code or PyCharm, you can check it out here: sourcery.ai/? * ---------------------------------------------------------------------------------------------------------- * 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! 🙏
@sourg_rage
@sourg_rage 3 года назад
Good tips, thanks! Personally, I don't like returning an expression directly without assigning to a variable. When you use a variable, it is easy to add a breakpoint without refactoring if you need to see the value before it is returned.
@patloeber
@patloeber 3 года назад
Good point! Yes this point is debatable, too :) I also like to use the variable in certain cases
@aleksandarandjelkovic8650
@aleksandarandjelkovic8650 2 года назад
It is as easy to add a breakpoint to a return statement even without a variable and evaluate the expression in a debugger, at least in a decent IDE. I don't think adding variables just for that is approved. Readability, on the other hand, is discussable.
@dennycrane2938
@dennycrane2938 Год назад
I can to make the same comment. It's easier for debugging
@meowvee
@meowvee 2 года назад
#4: `var=something; return var` has its place: debugging.
@adrien.cordonnier
@adrien.cordonnier 2 года назад
3:35 Tip #5 can be applied one more time. You can avoid the "else:" and corresponding indentation as you just returned in the previous if section.
@patloeber
@patloeber 2 года назад
yep thanks for the hint!
@Mutual_Information
@Mutual_Information 2 года назад
Really high information density on these vids. Very nice and not often seen.
@patloeber
@patloeber 2 года назад
thank you :)
@helovesdata8483
@helovesdata8483 2 года назад
I'm trying to become a data engineer and Python is my first coding language. My goal is to improve my programming skills and this video is super helpful to me.
@michealhall7776
@michealhall7776 2 года назад
Update: any(generator) --> Stops at the first true result of the generator, without processing the rest. Which is very useful and something I'm glad I now know Original comment: Can someone run that any( check I believe the whole generator is first evaluated then any checks the result. If so this is not what you want on most large datasets
@Yotanido
@Yotanido 3 года назад
3:57 I actually disagree with this one. Just checking the truthy-ness of a list is not always what you want. None is also falsy and is also recommended to be checked like this. What if you have a list that might be None? You now can't tell apart None and the empty list. Or, what if you have a list that shouldn't be None? If a None value makes it into your function somehow, it will be treated as if it were an empty list. It should just throw an exception instead, being given an invalid value. I always use "is None" and "len(x) == 0" checks to make it clear what exactly I am checking.
@patloeber
@patloeber 3 года назад
fair point! Checking for None and falsy can indeed be a pitfall in certain cases. I should have pointed this out...
@MaZe741
@MaZe741 2 года назад
if you have a list thats "None" then you dont have a list, why would you design your code like that?
@TheJacklwilliams
@TheJacklwilliams 2 года назад
@@patloeber Yeah I gotta jump in here with Random... To you and Yndostrul, from a newb, when will I have a list that is None? A little lost, even a simple example would help if you have a moment...
@dl662
@dl662 2 года назад
@@TheJacklwilliams random's argument is not very pythonic. more often than not we would want to default a list variable to None instead of [] bc how python evaluates its default arguments (e.g. def func(a_list=None)). It's very common to have a list that's None; and in this case, if we want to treat None and [] the same in a single condition, it's perfectly fine to write "if a_list:"; but if None and [] deserves their own case we would wanna be more explicit about checking "len(a_list) == 0" for the [] value.
@TheJacklwilliams
@TheJacklwilliams 2 года назад
@@dl662 Thanks Derek. I’m a complete newb and doing all I can to start out with good habits. Being pythonic and doing things in an optimum fashion are top of mind for me. I appreciate the explanation. Thank you.
@FORGIVE123N
@FORGIVE123N 2 года назад
Thanks a lot! Tips about "any" statement, assigning variables closer to their usage and guard statements to remove excessive indentation were very nice.
@CodingIsFun
@CodingIsFun 3 года назад
Hi Patrick, Great video as always! Thanks for sharing your knowledge! Also, your new Website design is outstanding. 👍 Beste Gruesse, Sven
@patloeber
@patloeber 3 года назад
Thank you! Just noticed you have a channel, too. Great work :)
@manojm9084
@manojm9084 Год назад
Thirst for learning . This channel is a good one to quench
@anneest
@anneest 2 года назад
Thanks for those tips, they are great! I have been refactoring a lot of my code lately, guess it shows I am progressing 😀 So refactoring tips: YES PLEASE! Great vid format as well 👍
@emi3955
@emi3955 Год назад
Great advice, did not know "any" . Thanks a lot
@thatoneuser8600
@thatoneuser8600 2 года назад
3:50 you can also inline the current_fashion local variable and rename the getter method, as well as getting rid of the else statement as the second if statement can also serve as a quasi-guard clause
@henryantwi8222
@henryantwi8222 9 месяцев назад
Thanks for these helpful tips. I'll start using them asap
@michealhall7776
@michealhall7776 2 года назад
I prefer to only have return per function, it's much cleaner and easier to debug. Returning results as the var IMO is best practice. It allows you to easily debug and read the code output = 'hello' * 2 return output #> 'hellohello' That advice is only true when dealing with large numpy arrays where creating the variable is time consuming.
@CupapiTG
@CupapiTG 5 месяцев назад
Nice! I didn't think of some of these, good tips.
2 года назад
these are true for any language, but good tips indeed
@TMQuest
@TMQuest 3 года назад
For number 5 (if expressions), then I think the if expressions can simplify code assuming that the code is not too long. For your example, I think that the shortening does not make it less readable. However, for if-else statements with longer assignments and conditions, the shorthand if expression can make the readability go way down (this is the same problem that long list comprehensions have). There is also the disadvantage that programmers from other languages can find the syntax a bit weird (although ternary operators still exist in e.g. JavaScript). I've purposefully avoided using the if expressions in my videos since I am not sure that everything knows about them. However, I think it would be good if more people knew :)
@nano7586
@nano7586 2 года назад
I have no idea why but I have always been doing these 8 things. I'm not even a good programmer but I guess a lot of trial & error gives you a bit of intuition about what seems more readable.
@estevaoyt
@estevaoyt 2 года назад
Python Engineer, you are amazing!
@nested9301
@nested9301 Год назад
python is beautiful than javascript but ilove them both xD nice video!
@RobertBrunhage
@RobertBrunhage 3 года назад
I don't even use Python but I am a simple man, If you upload I watch 😎
@patloeber
@patloeber 3 года назад
Haha thank you Robert, I don't use Flutter but I also watch a lot of your videos :D I can learn a lot from your video style!
@bobsalita3417
@bobsalita3417 3 года назад
Well balanced information. Concise. Thanks.
@patloeber
@patloeber 3 года назад
Glad you like it!
@SecretSanta-hg5yh
@SecretSanta-hg5yh 3 года назад
Why not use python black directly?
@Param3021
@Param3021 3 года назад
Thanks for the 8th tip, it is useful. Actually, your all tips are great and useful, Nice tips
@patloeber
@patloeber 3 года назад
Glad it was helpful!
@jagjitsinghramgarhia4766
@jagjitsinghramgarhia4766 2 года назад
I write nested if expressions and have written a nested list comprehension which was 3-4 levels deep, can't even debug it anymore, it just works and no one knows why now.
@lepidoptera9337
@lepidoptera9337 2 года назад
Why would you want to touch it, if it works? Unless you need to change it to accommodate new functionality, in which case it's better to rewrite it from scratch, leave it alone. Only amateurs mess with working systems.
@anelm.5127
@anelm.5127 3 года назад
You should definitely do more code refactorings
@patloeber
@patloeber 3 года назад
thanks for the suggestion :)
@AlphaWatt
@AlphaWatt 3 года назад
Excellent video. Thanks!
@patloeber
@patloeber 3 года назад
thanks for watching :)
@georgethornburg9259
@georgethornburg9259 2 года назад
Patrick, just subscribed too your channel. Love your work. I waa trying to find out where you're from. You sound Canadian ie Letterkenney vibe.
@patloeber
@patloeber 2 года назад
Haha thank you so much! I am German ;)
@Kevinschart
@Kevinschart Год назад
I have always found the "pythonic" version of code to be non intuitive. I like the traditional if statements.
@ChrisHalden007
@ChrisHalden007 2 года назад
Great tips. Thanks
@aligator329
@aligator329 4 месяца назад
Is sourcery and such extensions as copilot, recommended for a junior developer with 4 months experience?
@quixzotic
@quixzotic 3 года назад
Love the new video! 🔥
@patloeber
@patloeber 3 года назад
Glad to hear that!
@alex_linhares
@alex_linhares 2 года назад
Nice. Subscribed!
@patloeber
@patloeber 2 года назад
thank you :)
@SahilVerma-wm6ie
@SahilVerma-wm6ie 2 года назад
For #6 since you aren't computing anything inside the if statements, you can also just do return weather.is_raining or is_stylish Does this make sense to others?
@dcy3223
@dcy3223 2 года назад
thank your tips, your code font is also beautiful, i like it, Can you tell me which one font?
@OloNadTrolo
@OloNadTrolo 2 года назад
1:35 that's what type annotations and docstrings are for...
@satishshah9507
@satishshah9507 3 года назад
Sir, Please bring a series of #pywebio with best web app examples.
@ThePirateHistory
@ThePirateHistory 3 года назад
more pls.
@patloeber
@patloeber 3 года назад
I try to come up with some more :)
@mikapeltokorpi7671
@mikapeltokorpi7671 3 года назад
#5 How about: x = 1 if !(condition): x = 2 #7: last else is unnecessary.
@patloeber
@patloeber 3 года назад
yep that's another good option 👍
@toddkes5890
@toddkes5890 2 года назад
For me the first variable would be my 'default' condition, then the subsidiary conditions would be tested against. I.e. you are taking in a multi-line file and depending on the data you output it into different files. The default setting for a line of data would be "unknown record type", then I see if the record fits into a condition. If the record never fits any of the conditions, that record gets tossed into an "unknown format" file for the user to figure out, and if needed add more conditions to the program
@maahad767
@maahad767 2 года назад
I follow every tips except the inline condition due to readability and any(list) because I forget 😂
@AntonioSanchez-bi3nu
@AntonioSanchez-bi3nu 3 года назад
Nice.
@patloeber
@patloeber 3 года назад
thanks!
@PritishMishra
@PritishMishra 3 года назад
Which VS Code Theme you are using Sir?
@patloeber
@patloeber 3 года назад
Dracula!
@PritishMishra
@PritishMishra 3 года назад
@@patloeber Thanks for Quck Reply!
@iamdanfleser
@iamdanfleser 2 года назад
what theme do you use for the ide?
@patloeber
@patloeber 2 года назад
Material theme
@lmaootakedh
@lmaootakedh 2 года назад
Huh, I refactore my code alike.
@dbldekr
@dbldekr 2 года назад
Nah, just right click on random code and click your IDE's "refactor" button and trust that your code is better. I'm a profession programmer.
@SamrudhSwaminathan
@SamrudhSwaminathan 3 года назад
When You're First So You Don't Know Wut To Comment 🤔...
@patloeber
@patloeber 3 года назад
haha :D
@SamrudhSwaminathan
@SamrudhSwaminathan 3 года назад
@@patloeber Omg Thx Soo Much For REPLYING!! I'm a Big Fan Of U And Uve REALLY Helped Me With PyTorch!! TYSM
@patloeber
@patloeber 3 года назад
@@SamrudhSwaminathan Thank you! Glad you like it :)
@pythonicworld5026
@pythonicworld5026 2 года назад
Quick Python Refactoring Tips (30 Second videos): ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-D4xLPfetZRY.html
@Aldraz
@Aldraz Год назад
One line if statements should be forbidden.. it is so unreadable, ugly.. nah no thanks man.
@vasiliigulevich9202
@vasiliigulevich9202 2 года назад
These are all style fixes that do not affect readability or function. Are you changing working code just to satisfy your subjective sense of beauty? Please don't work for me.
@lepidoptera9337
@lepidoptera9337 2 года назад
Yep. You can tell that it's amateur hour. Worse, it's amateurs with OCD.
@Hecticam
@Hecticam 2 года назад
why u gotta sound like that bro
Далее
Avoid These BAD Practices in Python OOP
24:42
Просмотров 54 тыс.
25 nooby Python habits you need to ditch
9:12
Просмотров 1,8 млн
5 Good Python Habits
17:35
Просмотров 539 тыс.
50 Python Tips and Tricks for Beginners
42:41
Просмотров 66 тыс.
6 Tips to write BETTER For Loops in Python
9:19
Просмотров 249 тыс.
Make Your Python Code More Professional
19:43
Просмотров 65 тыс.
7 Python Code Smells to AVOID at All Costs
22:10
Просмотров 372 тыс.
10 Python Functions That Will Simplify Your Life
19:19
Why You Shouldn't Nest Your Code
8:30
Просмотров 2,7 млн
11 Tips And Tricks To Write Better Python Code
11:00
Просмотров 613 тыс.