0

i was wondering if there was a faster way to set those below

Max_age  = 21
Suzy_age = 21
Mia_age  = 21

why not writing

Max_age and Suzy_age and Mia_age = 21

or even

Max_age = Suzy_age = Mia_age = 21

I'm new to python and this kind of code seems really intuitive, but somehow there's no one liner like thoses ? am i wrong ? please tell me i'm wrong.

5
  • i get that we can put all object in a list, but i asked for a one liner. Commented Sep 25, 2019 at 10:30
  • Do you have to pay for lines of code? Commented Sep 25, 2019 at 10:32
  • Your last example is ok Commented Sep 25, 2019 at 10:33
  • 3
    Max_age = Suzy_age = Mia_age = 21 is already valid. Commented Sep 25, 2019 at 10:33
  • You should try checking if the examples you are thinking of work already, in your case the second one does. Commented Sep 25, 2019 at 10:34

3 Answers 3

1

The first one is invalid

a and b and c = 1

will cause an error, since a and b is a binary expression, which is a value, not a variable, so you can't assign to it.

the second one is valid

a = b = c = 1

will run as in the order

a = (b = (c = 1))

since the assignment is right-associative. The assignment c = 1 evaluates to 1, so that will be assigned to b, and so on.

However, in python it's common to write multiple assignments like

a, b, c = 1, 2, 3

which is called tuple unpacking

Whether that's preferable to having one assignment per line, is a matter of style and context.

Sign up to request clarification or add additional context in comments.

11 Comments

"assignment is right-associative. The assignment c = 1 returns 1, so that will be assigned to b, and so on." Umm, no, that's not how assignments work in python. They don't "return" anything, and they're executed left-to-right. (a is assigned first, then b, then c.)
I think it should be important to clarify, that a = (b = (c = 1)) is not a valid python statement as you can't assign a value to a global variable definition. a = b = c = 1 is a separate python syntax rule.
@Aran-Fey: a is assigned to the result of the expression b = .... So, the right expression has to be evaluated first. How could a be assigned first?
b = ... is not an expression at all. Assignments are statements, and they're executed left-to-right.
@blue_note As noted in the language reference, each target_list is assigned, left-to-right to the resulting object. The resulting object is 1 and each of a, b and c is a target list, meaning that a=b=c=1 sets a directly to 1, then b directly to 1 and c directly to 1, in that order. Parsing the expression using ast, or disassembling it using dis will both demonstrate that b is not set to the value of the subexpression c=1 for example.
|
0

You can do so:

Max_age, Suzy_age, Mia_age = 21, 21, 21

Comments

0
Max_age, Suzy_age, Mia_age = [21] * 3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.