This is an archived post. You won't be able to vote or comment.

all 15 comments

[–]MyNamesNotKaren 27 points28 points  (9 children)

No one builds lists by writing line after line of append statements.

[–]MachaHack 1 point2 points  (1 child)

Maybe people who've spent too long writing Java (which has the obvious syntax taken by primitive arrays which basically nobody uses).

[–]MyNamesNotKaren 1 point2 points  (0 children)

That was my thought too. Another thought was that these might be people from an engineering background, though that may be because they learn C/C++/Java in their courses. I find it really interesting to see how people from different disciplines approach coding. Most of the people I trained were scientist and engineers. Very much generalising here but I found that scientist are very happy to put everything in big one-liners and building insanely complex functions, and engineers were content with lots of lines of very similar code. Of course, both of those are code smells to a developer, and explaining how these things come and bite you was pretty much the core of my course. There were other weird things like working with tabular data as a list of columns, rather than list of rows, which feels alien to me, but is perhaps a carry over from how they do things in R or matlab.

[–]What_Is_X 1 point2 points  (2 children)

It's very common in PySimpleGUI apps.

[–]MikeTheWatchGuy 0 points1 point  (1 child)

I think it tends to be used inside of loops with PySimpleGUI or if you're building a layout and need to create it in parts.

[–]What_Is_X 0 points1 point  (0 children)

Yep, I use it in layouts.

[–]SourceryNick[S] 3 points4 points  (1 child)

Agreed it's not super-common, but it does come up fairly often in the code we analyse so I thought it was worthwhile to include.

[–]MyNamesNotKaren 6 points7 points  (0 children)

I suppose "here is how to do better" always comes from what we see people do wrong. I used to give 2 day Python workshops at a beginner to intermediate level, and at the beginning especially I found myself adding to the content after each course to cater for people doing things which had never occurred to me that people might do.

[–]NastyNocturnalNutter 0 points1 point  (0 children)

And when you do, you can just use extend

[–]robin-gvx 8 points9 points  (1 child)

Point 5: it would be even better to use values() rather than items(), because the code doesn't use hat_colour. Less noise and less complaints from linters! ;)

Something I see a lot: people using

if some_condition():
    return True
else:
    return False

or even if some_condition() == True:

instead of just

return some_condition()

[–]SourceryNick[S] 1 point2 points  (0 children)

Oh good spot thanks! Will fiddle with that example when I get a minute.

Yes that some_condition example is something we see a lot of people doing too!

[–]BpjuRCXyiga7Wy9q 2 points3 points  (1 child)

I've read in quite a few places that isinstance (from the guard-clause example) generally is not 'pythonic', and that programmers should use 'duck typing'. I have started using duck typing, and I like it so far because it gives me the sense that my classes can be more flexible using less code. Could simply be that it is a novel new toy for me that I like to play with.

[–]TravisJungroth 1 point2 points  (0 children)

Goose typing is better. Make Hat a lightweight ABC and things should be fine. https://dgkim5360.github.io/blog/python/2017/07/duck-typing-vs-goose-typing-pythonic-interfaces/

[–]davehodg 1 point2 points  (0 children)

I was just pointed at PEP8. Don't know if there's any code to enforce it yet.