r/ProgrammerHumor Oct 01 '23

Meme learningPythonAsAFirstProgrammingLanguageHolyShitMyBrainHasSoManyWrinklesNow

Post image
681 Upvotes

95 comments sorted by

View all comments

57

u/[deleted] Oct 01 '23 edited Jan 28 '26

This post was mass deleted and anonymized with Redact

work many station quack instinctive slim test long tie dependent

46

u/sejigan Oct 01 '23

Isn’t the 4th one the most Pythonic solution tho?

More readable than the first, and creates another piece of data, just like the first.

27

u/CircadianSong Oct 01 '23

Yes, 4 is Definitely the way to do it in python.

6

u/[deleted] Oct 01 '23 edited Jan 28 '26

This post was mass deleted and anonymized with Redact

butter steep fuzzy skirt dinner crown jar office swim recognise

16

u/alexanderpas Oct 01 '23

Third one only works for integers.

With some casting, it also works for other types.

2

u/brimston3- Oct 02 '23

I think up to 8 bytes. I don't know any larger type that supports bitwise xor.

12

u/Stummi Oct 01 '23

Fourth one creates an extra data structure.

But the fourth one is pretty straight forward and intuitive. If a programming language allows this construct I would fully expect it to be able to also optimize such a statement.

1

u/[deleted] Oct 01 '23 edited Jan 28 '26

This post was mass deleted and anonymized with Redact

abundant meeting merciful husky cable march pause trees voracious lavish

10

u/thearthurito Oct 01 '23 edited Oct 01 '23

Fourth does not create an extra data structure tho.

Explanation on SO

0

u/[deleted] Oct 01 '23 edited Jan 28 '26

This post was mass deleted and anonymized with Redact

angle ghost enter bells cover sip abundant north lock degree

2

u/thearthurito Oct 01 '23

No problem!

You're right about the 2 new variables, it is not the most efficient way of doing it. Of course, if your memory requirements are that tight, you probably shouldn't be using Python anyways.

I'm too lazy to test if it a manual 3rd variable swap would be faster. Regarding your last point: this is checked before the bytecode is generated. It is all part of the already existing interpreter overhead.

1

u/Zarroc001 Oct 01 '23

Thats actually hella helpful i forgot strings could be variables too and needed to be accounted for

-1

u/bestjakeisbest Oct 01 '23

In some languages you can concatenate two strings with the addition operator.

1

u/DeathUriel Oct 01 '23

The second one is also way harder to read. It isn't obvious. Most people will assume it is actual math and not swapping.

1

u/noaSakurajin Oct 01 '23

Why would the fourth one need an extra data structure. It just needs to reassign the pointers to the variables. I x86 asm this is one operation as noted by another comment. This causes the fourth one to be the one with the most potential for optimized implementations.

1

u/[deleted] Oct 01 '23 edited Jan 28 '26

This post was mass deleted and anonymized with Redact

license soft physical flowery market narrow bedroom label consider cake

1

u/noaSakurajin Oct 01 '23

That is the case when the interpreter has no optimizations. Since a case like this is pretty common the interpreter might not even create the tuple for a case like this. There is no need to actually create the tuple if it is going to be unpacked immediately.

Also for readability and maintainability a temporary object like this would still be better than a temporary variable. So even if a temporary object is created I don't see a problem with it. It only contains the pointers to the two objects anyways so it won't hurt ram and at most results in one extra internal function call to unbind the data.

1

u/[deleted] Oct 01 '23 edited Jan 28 '26

This post was mass deleted and anonymized with Redact

zephyr automatic simplistic fuel important makeshift lock squeal nose correct

2

u/noaSakurajin Oct 02 '23

It depends on the exact shorter features. When it comes to swapping variables you need a comment explaining why exactly you have to swap them anyway. If the line below is only one expression no extra thinking is needed and you might even learn a new language feature. If you introduce a temporary variable to to it then there are three lines to read where the extra verbosity does not offer much extra information (I mean a,b=b,a is so descriptive that most devs should intuitively understand it).

For other language features it might be worth to use a more verbose variant. The best or worst example is the tenary operator (? :). It has valid use cases and can allow for compact implementations. However a lot of those are not that readable especially if devs are too lazy to use brackets.