Multiple assignments in python
Clash Royale CLAN TAG#URR8PPP
up vote
8
down vote
favorite
I need a clear explanation here.
Why does the following code work ?
foo1 = foo1[0] = [0]
Ok, I know assignments are done left to right.
How does python understand foo1
is a list?
Btw I know foo1
ends up as [[...]]
its first element being itself.
python
add a comment |Â
up vote
8
down vote
favorite
I need a clear explanation here.
Why does the following code work ?
foo1 = foo1[0] = [0]
Ok, I know assignments are done left to right.
How does python understand foo1
is a list?
Btw I know foo1
ends up as [[...]]
its first element being itself.
python
2
Depends on whatfoo1
was originally. If looks like it was a list with at least one element in it.
â Makoto
55 mins ago
2
Is the question purely theoretical, or you found any use to this kind of construct? @Makoto it does not depend on the previous use / definition offoo1
.
â norok2
52 mins ago
@norok2: Yeah it does. You can't referencefoo1[0]
iffoo1
isn't indexable (e.g. like an object which doesn't have that property overridden or a number).
â Makoto
47 mins ago
Theoretical question.
â Jeremie
46 mins ago
1
@Makoto Please copy paste OP's code into a fresh interpreter session.
â timgeb
29 mins ago
add a comment |Â
up vote
8
down vote
favorite
up vote
8
down vote
favorite
I need a clear explanation here.
Why does the following code work ?
foo1 = foo1[0] = [0]
Ok, I know assignments are done left to right.
How does python understand foo1
is a list?
Btw I know foo1
ends up as [[...]]
its first element being itself.
python
I need a clear explanation here.
Why does the following code work ?
foo1 = foo1[0] = [0]
Ok, I know assignments are done left to right.
How does python understand foo1
is a list?
Btw I know foo1
ends up as [[...]]
its first element being itself.
python
python
edited 36 mins ago
timgeb
37k104875
37k104875
asked 59 mins ago
Jeremie
643
643
2
Depends on whatfoo1
was originally. If looks like it was a list with at least one element in it.
â Makoto
55 mins ago
2
Is the question purely theoretical, or you found any use to this kind of construct? @Makoto it does not depend on the previous use / definition offoo1
.
â norok2
52 mins ago
@norok2: Yeah it does. You can't referencefoo1[0]
iffoo1
isn't indexable (e.g. like an object which doesn't have that property overridden or a number).
â Makoto
47 mins ago
Theoretical question.
â Jeremie
46 mins ago
1
@Makoto Please copy paste OP's code into a fresh interpreter session.
â timgeb
29 mins ago
add a comment |Â
2
Depends on whatfoo1
was originally. If looks like it was a list with at least one element in it.
â Makoto
55 mins ago
2
Is the question purely theoretical, or you found any use to this kind of construct? @Makoto it does not depend on the previous use / definition offoo1
.
â norok2
52 mins ago
@norok2: Yeah it does. You can't referencefoo1[0]
iffoo1
isn't indexable (e.g. like an object which doesn't have that property overridden or a number).
â Makoto
47 mins ago
Theoretical question.
â Jeremie
46 mins ago
1
@Makoto Please copy paste OP's code into a fresh interpreter session.
â timgeb
29 mins ago
2
2
Depends on what
foo1
was originally. If looks like it was a list with at least one element in it.â Makoto
55 mins ago
Depends on what
foo1
was originally. If looks like it was a list with at least one element in it.â Makoto
55 mins ago
2
2
Is the question purely theoretical, or you found any use to this kind of construct? @Makoto it does not depend on the previous use / definition of
foo1
.â norok2
52 mins ago
Is the question purely theoretical, or you found any use to this kind of construct? @Makoto it does not depend on the previous use / definition of
foo1
.â norok2
52 mins ago
@norok2: Yeah it does. You can't reference
foo1[0]
if foo1
isn't indexable (e.g. like an object which doesn't have that property overridden or a number).â Makoto
47 mins ago
@norok2: Yeah it does. You can't reference
foo1[0]
if foo1
isn't indexable (e.g. like an object which doesn't have that property overridden or a number).â Makoto
47 mins ago
Theoretical question.
â Jeremie
46 mins ago
Theoretical question.
â Jeremie
46 mins ago
1
1
@Makoto Please copy paste OP's code into a fresh interpreter session.
â timgeb
29 mins ago
@Makoto Please copy paste OP's code into a fresh interpreter session.
â timgeb
29 mins ago
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
8
down vote
Because
foo1 = foo1[0] = [0]
is equivalent to
temp = [0]
foo1 = temp
foo1[0] = temp
it first evaluates expression and then assigns from left to right.
assigns the single resulting object to each of the target lists, from left to right
from here: https://docs.python.org/3/reference/simple_stmts.html#assignment-statements
Update: also see answers to similar question here: Multiple assignment and evaluation order in Python
add a comment |Â
up vote
0
down vote
Python variables know their types based on the type of variable assigned to it. It is a dynamically types language. In your code, the interpreter sees foo1 = foo1[0] = [0]
and it fins a value at the end, which is [0]
. It is a list with one element 0
. Now, this gets assigned to the first element of the list foo1
through foo1[0] = [0]
. But since foo1
is already declared, it creates an object which has a pointer to itself, and hence foo1
gets self-referenced infinitely, with the innermost list having 0.
The structure of the list foo1
will be the same when the code is foo1 = foo1[0]
.
The object foo1
has entered an infinite self-referenced loop.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
8
down vote
Because
foo1 = foo1[0] = [0]
is equivalent to
temp = [0]
foo1 = temp
foo1[0] = temp
it first evaluates expression and then assigns from left to right.
assigns the single resulting object to each of the target lists, from left to right
from here: https://docs.python.org/3/reference/simple_stmts.html#assignment-statements
Update: also see answers to similar question here: Multiple assignment and evaluation order in Python
add a comment |Â
up vote
8
down vote
Because
foo1 = foo1[0] = [0]
is equivalent to
temp = [0]
foo1 = temp
foo1[0] = temp
it first evaluates expression and then assigns from left to right.
assigns the single resulting object to each of the target lists, from left to right
from here: https://docs.python.org/3/reference/simple_stmts.html#assignment-statements
Update: also see answers to similar question here: Multiple assignment and evaluation order in Python
add a comment |Â
up vote
8
down vote
up vote
8
down vote
Because
foo1 = foo1[0] = [0]
is equivalent to
temp = [0]
foo1 = temp
foo1[0] = temp
it first evaluates expression and then assigns from left to right.
assigns the single resulting object to each of the target lists, from left to right
from here: https://docs.python.org/3/reference/simple_stmts.html#assignment-statements
Update: also see answers to similar question here: Multiple assignment and evaluation order in Python
Because
foo1 = foo1[0] = [0]
is equivalent to
temp = [0]
foo1 = temp
foo1[0] = temp
it first evaluates expression and then assigns from left to right.
assigns the single resulting object to each of the target lists, from left to right
from here: https://docs.python.org/3/reference/simple_stmts.html#assignment-statements
Update: also see answers to similar question here: Multiple assignment and evaluation order in Python
edited 35 mins ago
answered 40 mins ago
Eduard
35816
35816
add a comment |Â
add a comment |Â
up vote
0
down vote
Python variables know their types based on the type of variable assigned to it. It is a dynamically types language. In your code, the interpreter sees foo1 = foo1[0] = [0]
and it fins a value at the end, which is [0]
. It is a list with one element 0
. Now, this gets assigned to the first element of the list foo1
through foo1[0] = [0]
. But since foo1
is already declared, it creates an object which has a pointer to itself, and hence foo1
gets self-referenced infinitely, with the innermost list having 0.
The structure of the list foo1
will be the same when the code is foo1 = foo1[0]
.
The object foo1
has entered an infinite self-referenced loop.
add a comment |Â
up vote
0
down vote
Python variables know their types based on the type of variable assigned to it. It is a dynamically types language. In your code, the interpreter sees foo1 = foo1[0] = [0]
and it fins a value at the end, which is [0]
. It is a list with one element 0
. Now, this gets assigned to the first element of the list foo1
through foo1[0] = [0]
. But since foo1
is already declared, it creates an object which has a pointer to itself, and hence foo1
gets self-referenced infinitely, with the innermost list having 0.
The structure of the list foo1
will be the same when the code is foo1 = foo1[0]
.
The object foo1
has entered an infinite self-referenced loop.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Python variables know their types based on the type of variable assigned to it. It is a dynamically types language. In your code, the interpreter sees foo1 = foo1[0] = [0]
and it fins a value at the end, which is [0]
. It is a list with one element 0
. Now, this gets assigned to the first element of the list foo1
through foo1[0] = [0]
. But since foo1
is already declared, it creates an object which has a pointer to itself, and hence foo1
gets self-referenced infinitely, with the innermost list having 0.
The structure of the list foo1
will be the same when the code is foo1 = foo1[0]
.
The object foo1
has entered an infinite self-referenced loop.
Python variables know their types based on the type of variable assigned to it. It is a dynamically types language. In your code, the interpreter sees foo1 = foo1[0] = [0]
and it fins a value at the end, which is [0]
. It is a list with one element 0
. Now, this gets assigned to the first element of the list foo1
through foo1[0] = [0]
. But since foo1
is already declared, it creates an object which has a pointer to itself, and hence foo1
gets self-referenced infinitely, with the innermost list having 0.
The structure of the list foo1
will be the same when the code is foo1 = foo1[0]
.
The object foo1
has entered an infinite self-referenced loop.
answered 15 mins ago
a_ran
387
387
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52467743%2fmultiple-assignments-in-python%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
Depends on what
foo1
was originally. If looks like it was a list with at least one element in it.â Makoto
55 mins ago
2
Is the question purely theoretical, or you found any use to this kind of construct? @Makoto it does not depend on the previous use / definition of
foo1
.â norok2
52 mins ago
@norok2: Yeah it does. You can't reference
foo1[0]
iffoo1
isn't indexable (e.g. like an object which doesn't have that property overridden or a number).â Makoto
47 mins ago
Theoretical question.
â Jeremie
46 mins ago
1
@Makoto Please copy paste OP's code into a fresh interpreter session.
â timgeb
29 mins ago