Quick Python Refactoring Tips

Quick Python Refactoring Tips

Patrick Loeber

2 года назад

108,783 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Kevin Schart
Kevin Schart - 27.05.2023 00:01

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

Ответить
Aldraz
Aldraz - 17.01.2023 00:58

One line if statements should be forbidden.. it is so unreadable, ugly.. nah no thanks man.

Ответить
Emi
Emi - 15.11.2022 15:19

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

Ответить
manoj m
manoj m - 08.11.2022 21:14

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

Ответить
nested
nested - 25.10.2022 17:15

python is beautiful than javascript but ilove them both xD nice video!

Ответить
He Loves Data
He Loves Data - 08.08.2022 13:38

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.

Ответить
Na No
Na No - 01.04.2022 23:04

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.

Ответить
lmaoooo
lmaoooo - 01.04.2022 20:57

Huh, I refactore my code alike.

Ответить
Estevao Fonseca
Estevao Fonseca - 02.03.2022 22:45

Python Engineer, you are amazing!

Ответить
Anne E
Anne E - 28.12.2021 20:15

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 👍

Ответить
Embru
Embru - 26.11.2021 02:55

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

Ответить
Jagjit Singh Ramgarhia
Jagjit Singh Ramgarhia - 25.11.2021 20:51

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.

Ответить
Ádám Márton
Ádám Márton - 13.11.2021 12:48

these are true for any language, but good tips indeed

Ответить
George Thornburg
George Thornburg - 13.11.2021 09:58

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.

Ответить
Jeb
Jeb - 12.11.2021 22:52

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.

Ответить
Vasilii Gulevich
Vasilii Gulevich - 08.11.2021 09:43

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.

Ответить
Sahil Verma
Sahil Verma - 05.11.2021 13:15

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?

Ответить
Hecticam
Hecticam - 02.11.2021 13:00

why u gotta sound like that bro

Ответить
Dan Later
Dan Later - 02.11.2021 11:23

what theme do you use for the ide?

Ответить
ZobDeMouche007
ZobDeMouche007 - 31.10.2021 15:08

Great tips. Thanks

Ответить
Mutual Information
Mutual Information - 23.10.2021 19:44

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

Ответить
德成中区
德成中区 - 22.10.2021 05:06

thank your tips, your code font is also beautiful, i like it, Can you tell me which one font?

Ответить
Hai Vu
Hai Vu - 19.10.2021 21:23

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

Ответить
linharesvids
linharesvids - 18.10.2021 02:46

Nice. Subscribed!

Ответить
Mohammad Abdul Ahad
Mohammad Abdul Ahad - 16.10.2021 13:25

I follow every tips except the inline condition due to readability and any(list) because I forget 😂

Ответить
micheal hall
micheal hall - 15.10.2021 12:05

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.

Ответить
micheal hall
micheal hall - 15.10.2021 12:00

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

Ответить
SKB ___
SKB ___ - 08.09.2021 05:44

Excellent video. Thanks!

Ответить
AM.
AM. - 26.08.2021 03:07

You should definitely do more code refactorings

Ответить
Secret Santa
Secret Santa - 03.08.2021 23:43

Why not use python black directly?

Ответить
TM Quest
TM Quest - 13.07.2021 01:08

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

Ответить
Mika Peltokorpi
Mika Peltokorpi - 12.07.2021 12:29

#5 How about:

x = 1
if !(condition):
x = 2

#7: last else is unnecessary.

Ответить
Bob Salita
Bob Salita - 12.07.2021 11:47

Well balanced information. Concise. Thanks.

Ответить
Coding Is Fun
Coding Is Fun - 12.07.2021 06:45

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

Ответить
Quixzotic
Quixzotic - 12.07.2021 04:45

Love the new video! 🔥

Ответить
Parampreet Singh
Parampreet Singh - 11.07.2021 18:37

Thanks for the 8th tip, it is useful.
Actually, your all tips are great and useful, Nice tips

Ответить
Robert Brunhage
Robert Brunhage - 11.07.2021 18:07

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

Ответить
Pritish Mishra
Pritish Mishra - 11.07.2021 18:03

Which VS Code Theme you are using Sir?

Ответить
Sourg
Sourg - 11.07.2021 17:47

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.

Ответить
Pirate Stories
Pirate Stories - 11.07.2021 17:26

more pls.

Ответить
Antonio Sánchez
Antonio Sánchez - 11.07.2021 16:48

Nice.

Ответить
Samrudh Swaminathan
Samrudh Swaminathan - 11.07.2021 16:45

When You're First So You Don't Know Wut To Comment 🤔...

Ответить