Quick Python Refactoring Tips

  Рет қаралды 108,786

Patrick Loeber

Patrick Loeber

2 жыл бұрын

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/?... *
Get my Free NumPy Handbook:
www.python-engineer.com/numpy...
⭐ Join Our Discord : / discord
📓 ML Notebooks available on Patreon:
/ patrickloeber
If you enjoyed this video, please subscribe to the channel:
▶️ : / @patloeber
Resources: sourcery.ai/blog/explaining-r...
~~~~~~~~~~~~~~~ CONNECT ~~~~~~~~~~~~~~~
🖥️ Website: www.python-engineer.com
🐦 Twitter - / patloeber
✉️ Newsletter - www.python-engineer.com/newsl...
📸 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! 🙏

Пікірлер: 83
@patloeber
@patloeber 2 жыл бұрын
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! 🙏
@meowvee
@meowvee 2 жыл бұрын
#4: `var=something; return var` has its place: debugging.
@sourg_rage
@sourg_rage 2 жыл бұрын
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 2 жыл бұрын
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 8 ай бұрын
I can to make the same comment. It's easier for debugging
@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.
@CupapiTG
@CupapiTG 22 күн бұрын
Nice! I didn't think of some of these, good tips.
@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!
@henryantwi8222
@henryantwi8222 5 ай бұрын
Thanks for these helpful tips. I'll start using them asap
@manojm9084
@manojm9084 Жыл бұрын
Thirst for learning . This channel is a good one to quench
@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
@emi3955
@emi3955 Жыл бұрын
Great advice, did not know "any" . Thanks a lot
@Mutual_Information
@Mutual_Information 2 жыл бұрын
Really high information density on these vids. Very nice and not often seen.
@patloeber
@patloeber 2 жыл бұрын
thank you :)
@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 👍
@bobsalita3417
@bobsalita3417 2 жыл бұрын
Well balanced information. Concise. Thanks.
@patloeber
@patloeber 2 жыл бұрын
Glad you like it!
@ChrisHalden007
@ChrisHalden007 2 жыл бұрын
Great tips. Thanks
@estevaoyt
@estevaoyt 2 жыл бұрын
Python Engineer, you are amazing!
@helovesdata8483
@helovesdata8483 Жыл бұрын
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
@TMQuest
@TMQuest 2 жыл бұрын
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 :)
@AlphaWatt
@AlphaWatt 2 жыл бұрын
Excellent video. Thanks!
@patloeber
@patloeber 2 жыл бұрын
thanks for watching :)
@Param3021
@Param3021 2 жыл бұрын
Thanks for the 8th tip, it is useful. Actually, your all tips are great and useful, Nice tips
@patloeber
@patloeber 2 жыл бұрын
Glad it was helpful!
@quixzotic
@quixzotic 2 жыл бұрын
Love the new video! 🔥
@patloeber
@patloeber 2 жыл бұрын
Glad to hear that!
@CodingIsFun
@CodingIsFun 2 жыл бұрын
Hi Patrick, Great video as always! Thanks for sharing your knowledge! Also, your new Website design is outstanding. 👍 Beste Gruesse, Sven
@patloeber
@patloeber 2 жыл бұрын
Thank you! Just noticed you have a channel, too. Great work :)
@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.
@alex_linhares
@alex_linhares 2 жыл бұрын
Nice. Subscribed!
@patloeber
@patloeber 2 жыл бұрын
thank you :)
@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.
2 жыл бұрын
these are true for any language, but good tips indeed
@Yotanido
@Yotanido 2 жыл бұрын
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 2 жыл бұрын
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.
@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 ;)
@nested9301
@nested9301 Жыл бұрын
python is beautiful than javascript but ilove them both xD nice video!
@anelm.5127
@anelm.5127 2 жыл бұрын
You should definitely do more code refactorings
@patloeber
@patloeber 2 жыл бұрын
thanks for the suggestion :)
@aligator329
@aligator329 2 күн бұрын
Is sourcery and such extensions as copilot, recommended for a junior developer with 4 months experience?
@dcy3223
@dcy3223 2 жыл бұрын
thank your tips, your code font is also beautiful, i like it, Can you tell me which one font?
@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?
@AntonioSanchez-bi3nu
@AntonioSanchez-bi3nu 2 жыл бұрын
Nice.
@patloeber
@patloeber 2 жыл бұрын
thanks!
@SecretSanta-hg5yh
@SecretSanta-hg5yh 2 жыл бұрын
Why not use python black directly?
@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.
@satishshah9507
@satishshah9507 2 жыл бұрын
Sir, Please bring a series of #pywebio with best web app examples.
@ThePirateHistory
@ThePirateHistory 2 жыл бұрын
more pls.
@patloeber
@patloeber 2 жыл бұрын
I try to come up with some more :)
@xellossek
@xellossek 2 жыл бұрын
1:35 that's what type annotations and docstrings are for...
@RobertBrunhage
@RobertBrunhage 2 жыл бұрын
I don't even use Python but I am a simple man, If you upload I watch 😎
@patloeber
@patloeber 2 жыл бұрын
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!
@Kevinschart
@Kevinschart Жыл бұрын
I have always found the "pythonic" version of code to be non intuitive. I like the traditional if statements.
@danlater
@danlater 2 жыл бұрын
what theme do you use for the ide?
@patloeber
@patloeber 2 жыл бұрын
Material theme
@PritishMishra
@PritishMishra 2 жыл бұрын
Which VS Code Theme you are using Sir?
@patloeber
@patloeber 2 жыл бұрын
Dracula!
@PritishMishra
@PritishMishra 2 жыл бұрын
@@patloeber Thanks for Quck Reply!
@maahad767
@maahad767 2 жыл бұрын
I follow every tips except the inline condition due to readability and any(list) because I forget 😂
@pythonicworld5026
@pythonicworld5026 2 жыл бұрын
Quick Python Refactoring Tips (30 Second videos): kzbin.info/www/bejne/emXbfYOcmtmNiLs
@mikapeltokorpi7671
@mikapeltokorpi7671 2 жыл бұрын
#5 How about: x = 1 if !(condition): x = 2 #7: last else is unnecessary.
@patloeber
@patloeber 2 жыл бұрын
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
@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 2 жыл бұрын
When You're First So You Don't Know Wut To Comment 🤔...
@patloeber
@patloeber 2 жыл бұрын
haha :D
@SamrudhSwaminathan
@SamrudhSwaminathan 2 жыл бұрын
@@patloeber Omg Thx Soo Much For REPLYING!! I'm a Big Fan Of U And Uve REALLY Helped Me With PyTorch!! TYSM
@patloeber
@patloeber 2 жыл бұрын
@@SamrudhSwaminathan Thank you! Glad you like it :)
@Hecticam
@Hecticam 2 жыл бұрын
why u gotta sound like that bro
@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.
@Aldraz
@Aldraz Жыл бұрын
One line if statements should be forbidden.. it is so unreadable, ugly.. nah no thanks man.
8 Python Coding Tips - From The Google Python Style Guide
17:12
ArjanCodes
Рет қаралды 152 М.
Super Quick Python Refactoring Tips
5:13
Patrick Loeber
Рет қаралды 41 М.
He Threw A Banana Peel At A Child🍌🙈😿
00:27
Giggle Jiggle
Рет қаралды 17 МЛН
ISSEI funny story😂😂😂Strange World | Magic Lips💋
00:36
ISSEI / いっせい
Рет қаралды 160 МЛН
Balloon Pop Racing Is INTENSE!!!
01:00
A4
Рет қаралды 17 МЛН
10 Python Basics You Should Know!
10:08
Patrick Loeber
Рет қаралды 78 М.
5 Tips To Write Better Python Functions
15:59
Indently
Рет қаралды 74 М.
21 MORE nooby Python habits
9:55
mCoding
Рет қаралды 111 М.
Knowing this can save you HOURS of debugging! (5 Python Pitfalls)
10:45
4 Tips for Refactoring Your Code for Readability
14:58
Infallible Code
Рет қаралды 30 М.
Python dataclasses will save you HOURS, also featuring attrs
8:50
Beautiful Python Refactoring II - Conor Hoekstra - code::dive 2022
54:06
code::dive conference
Рет қаралды 3,6 М.
7 Python Code Smells: Olfactory Offenses To Avoid At All Costs
22:10
11 Tips And Tricks To Write Better Python Code
11:00
Patrick Loeber
Рет қаралды 598 М.
Compiled Python is FAST
12:57
Doug Mercer
Рет қаралды 82 М.