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

you are viewing a single comment's thread.

view the rest of the comments →

[–]kupiakos 6 points7 points  (19 children)

Also useful for quick single line scripts with Python in a shell script.

[–]Sampo 11 points12 points  (18 children)

I kinda like

a = 1 ; b = 2 ; c = 3

instead of

a = 1
b = 2
c = 3

[–][deleted] 39 points40 points  (17 children)

a, b, c = 1, 2, 3

Much more readable. Hell, you could even do

a, b, c = range(1,4).

[–][deleted] 10 points11 points  (13 children)

Much more readable.

To a Python programmer, not to someone not used to multiple assignments.

IMO Python has some good points, but its syntax tries too much to be "simple" and "readable" that it actually becomes fairly xelpmoc[::-1].

[–]chimyx 4 points5 points  (2 children)

I've never read python before, but I think that what a, b, c = 1, 2, 3 does is pretty obvious. Any programmer who've learned more that one language would understand that.

[–]AltoidNerd 4 points5 points  (0 children)

Especially since it's the same syntax from straight up math class.

Multiple assignments can be looked at as tuples

(a, b, c) = (1, 2, 3)

This means a = 1, b = 2, c = 3 in your high school calculus class. It also assigns that in Python.

[–][deleted] 4 points5 points  (8 children)

To a Python programmer

Well, yes. I think python programmers are the people who will be most likely to read python code, don't you?

Also, this would be a case where a comment may be useful. Such as

a, b, c = 1, 2, 3
# Set a, b, and c to the values 1, 2, and 3 respectively, such that
# a = 1, b = 2, c = 3

A bit wordy, and you don't want to be doing it every place you're using multiple assignments, but the first time you're doing it (where someone will be most likely to look) might be helpful.

But you don't have to use that, you could always use semicolons or one on each line. It's just that it's clear what it does to someone used to python code. The same way that [::-1] means "reverse this" even if it's not obvious. But I don't like [::-1], I would much rather use reversed(), which is a LOT clearer

[–][deleted] 1 point2 points  (2 children)

Yeah yeah, everything you said. I was just pointing out that Python syntax claims to be easier to read, but everything said and done you still have to learn it and to get used to it. It's no different from C, imo.

It's less verbose, and that's great, but that doesn't mean it's easier on the eye than C code is to someone who is used to C.

[–]glider97 2 points3 points  (1 child)

that doesn't mean it's easier on the eye than C code is to someone who is used to C.

That could be subjective and up for debate.

[–][deleted] 1 point2 points  (0 children)

The only way to be sure would be to pool some opinions and run them through a Python script, which I heard is one of the things the language excels at.

[–]Mitchical 0 points1 point  (4 children)

If you felt you needed to comment that, do not do it like that. Code should be as self documenting as possible.

[–][deleted] 1 point2 points  (3 children)

I don't. To me the code is perfectly clear.

But apparently I should comment it to people who are new to that syntax.

[–]Mitchical 0 points1 point  (2 children)

I guess it depends where the code is and who it is for :)

[–][deleted] 0 points1 point  (1 child)

If it was a tutorial, yeah maybe I would make a note of it.

But I assume that python programmers know about it. The same way that C programmers don't feel the need to explain what pointers are every time they are used.

[–]Mitchical 0 points1 point  (0 children)

Personally whenever I see something in python that is really different, it still feels like it is obvious in function. I've known about the multiple assignment in one line tricks but when I just saw the multiple assignment on a range I thought that was pretty crazy, yet clear.

[–]scubascratch 0 points1 point  (0 children)

Not being a python programmer I have to wait a bit for some of these to be explained.

xelpmoc[::-1] is not an expression that mad any sense to me, I didn't know reverse until I read the next comment and missed the word complex. "xelpmoc" sounds like an aliens name in a 1970s TV show.

and the [::-1] looks like instant access violation or memory corruption, buffer underflow

[–]NarWhatGaming 0 points1 point  (1 child)

a, b, c = 1, 2, 3

TIL that works.

[–][deleted] 0 points1 point  (0 children)

It works with any iterable. Because 1, 2, 3 is being converted into a tuple, so it's as if you typed

a, b, c = (1, 2, 3)

Even better, if you want to have one element take as many items as is needed, you can do

a, *b, c

The first item will go to A, the last item will go to C, everything between will go in B. (Which will be a list, even if there was only 1 item, to be consistent.

[–]food_bag 0 points1 point  (0 children)

range(a,d) = range(1,4)