21 MORE nooby Python habits

21 MORE nooby Python habits

mCoding

1 год назад

110,843 Просмотров

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


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

Андрей Прохоров
Андрей Прохоров - 20.09.2023 12:52

I once was creating a parser in python and, naturally, was using bs4 for finding stuff. This, however, resulted in a bottleneck and replacing bs4 with regex made a 12x speed improvement from a couple of seconds for each operation to a couple dozens of milliseconds

Ответить
shandows15
shandows15 - 15.09.2023 08:38

My takeaway as a non-python programmer: Oh dear god, python has support for multiple inheritance.

Ответить
Evan Suits
Evan Suits - 31.08.2023 20:07

This is Good Stuff! 17 years of assembler followed by 20+ years of C have left me unprepared for these newfangled gadgets. Thanks ever so much for helping me see through the blur ....

Ответить
Regic
Regic - 30.08.2023 22:47

#11 I don't actually like @property, when someone creates a setter it usually has a side effect. If I'm looking at where it is used, `obj.x = something` it is really surprising when I find out that this does something more than just changing the x variable. But if I see an `obj.set_x(something)` that immediately raises the alarm: `set_x()` probably does something more - why would anyone bother to write this function otherwise?

I suppose if your setter really has no side effect it is safe to use property, but in my experience that is rare (maybe you want to log where it changed or something like that, it's fine). Usually however, it re-computes related values or notifies other objects that a value has been updated - you should be aware of these.

Ответить
Mikko
Mikko - 24.08.2023 21:39

What's the benefit of StringIO over a list and "".join(the_list)?

Ответить
Franciszek Moszczuk
Franciszek Moszczuk - 18.08.2023 17:42

"Concatenating strings with plus" I doubt it gives a meaningful performance improvement if you have string concatenation once or twice in a program. Sure, when I will be writing extra long strings I might actually use this, otherwise it's just unnecessary code obfuscation.

Ответить
RedShipsofSpainAgain
RedShipsofSpainAgain - 30.07.2023 18:52

+1 to #9: "Using single letter variables". For real: nobody is going to understand what is meant by x or y or t or v.

PSA: USE DESCRIPTIVE VARIABLE NAMES!!! It takes one second longer to type out a descriptive variable name and your future self will thank you when you come back months later to read your code and wonder, "Why the hell did I call this 'x'? What does 'x' mean in this code?"

Ответить
vorpal22
vorpal22 - 26.06.2023 14:57

Do the separators matter in pathlib? I always use os.path.join to create the path string, since I have no idea if / will be translated to \ on vomits Windows.

The variable thing is very common amongst scientists who use Python. I find a lot of them write Python as they would C. We had a science staff member writing incomprehensible code with variables named iptr which in his mind, meant index of partner. slaps head Reading his code was hard enough because we software engineers weren't familiar with the scientific formulae in that branch of science, so I would just go through and change all the variable names after he merged a PR because it was faster than asking him to do it.

He would also use dictionaries to pass everything around and had to be taught dataclasses. Thank the Python gods for dataclasses.

Ответить
PripyatSoldier
PripyatSoldier - 20.06.2023 10:15

#3 paths as strings, and even recommends Pathlib instead of os.path
#17 „defines path as raw string“

Eh?

Ответить
Isaac T the Scratcher
Isaac T the Scratcher - 16.06.2023 17:16

captions are a little JANK...

Ответить
Secret Chest
Secret Chest - 12.06.2023 21:52

Pygame does.#21

Ответить
blake james
blake james - 05.06.2023 03:07

How does he highlight in blue?

Ответить
Frump Beard
Frump Beard - 05.05.2023 05:05

To add onto the one-letter variable names: if you want to use multi-cursor on the variable later, you're boned because that one letter shows up all over the place.

Ответить
vuvu700
vuvu700 - 04.05.2023 14:18

Sometimes, even after 8 years of python, you have some nooby habits :) . As always great video !

Ответить
Aaron Thompson
Aaron Thompson - 04.05.2023 04:59

# Long_computation... 42. Love it xD

Ответить
grawss
grawss - 01.05.2023 15:56

On #5, searching for answers gives some mixed messages, where the immediate evidence is that += is faster than the alternatives in most cases due to some python interpreter trickery, but in testing it's pretty clear that io.StringIO is substantially faster. For 50,000 appends I get 0.00428s using perf_counter, and with StringIO I get 0.00299s. That enormously faster.

Ответить
Tushar Sadhwani
Tushar Sadhwani - 28.04.2023 23:00

More people need to know about ast.literal_eval(), as it will probably do what you want from eval() while being completely safe.

Ответить
Jullien
Jullien - 25.04.2023 16:21

Hola! Puedes explicar él biblioteca io? Gracias. Saludos de Brasil.

Ответить
Діма Красько
Діма Красько - 31.03.2023 19:25

As far as I know, something was changed in string concatenation. Concatenation of 10 million elements with StringIO beats basic string concatenation just by 10%

Ответить
Сергей Ширнин
Сергей Ширнин - 27.03.2023 15:49

good ones. i see no point abt map/filter tho.

Ответить
namensklauer
namensklauer - 25.03.2023 16:50

i noticed that for #15 the first line was actually line 15, and i think it would be a neat little easter egg if the first visible line would always match the current number.

Ответить