Quick Python Refactoring Tips

preview_player
Показать описание
In this Python Tutorial I show you 8 quick Python refactoring tips for cleaner and more Pythonic code.

Get my Free NumPy Handbook:

📓 ML Notebooks available on Patreon:

If you enjoyed this video, please subscribe to the channel:

~~~~~~~~~~~~~~~ CONNECT ~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~ SUPPORT ME ~~~~~~~~~~~~~~

#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! 🙏
Рекомендации по теме
Комментарии
Автор



* 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! 🙏

patloeber
Автор

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.

sourg_rage
Автор

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.

helovesdata
Автор

#4: `var=something; return var` has its place: debugging.

meowvee
Автор

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.

adrien.cordonnier
Автор

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

michealhall
Автор

Really high information density on these vids. Very nice and not often seen.

Mutual_Information
Автор

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.

Yotanido
Автор

Thirst for learning . This channel is a good one to quench

manojm
Автор

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.

michealhall
Автор

Thanks a lot! Tips about "any" statement, assigning variables closer to their usage and guard statements to remove excessive indentation were very nice.

FORGIVEN
Автор

Hi Patrick,
Great video as always! Thanks for sharing your knowledge!
Also, your new Website design is outstanding. 👍
Beste Gruesse,
Sven

CodingIsFun
Автор

I don't even use Python but I am a simple man, If you upload I watch 😎

RobertBrunhage
Автор

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

thatoneuser
Автор

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.

nano
Автор

I have always found the "pythonic" version of code to be non intuitive. I like the traditional if statements.

Kevinschart
Автор

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 :)

TMQuest
Автор

Great advice, did not know "any" . Thanks a lot

emi
Автор

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 👍

anneest
Автор

these are true for any language, but good tips indeed