Questions tagged [multiple-assignment]

35 questions
74
votes
11 answers

Multiple assignment and evaluation order in Python

What is the difference between the following Python expressions: # First: x,y = y,x+y # Second: x = y y = x+y First gives different results than Second. e.g., First: >>> x = 1 >>> y = 2 >>> x,y = y,x+y >>> x 2 >>> y 3 Second: >>> x = 1 >>> y =…
30
votes
5 answers

How to assign multiple variables at once in JavaScript?

Is there any way to perform multiple assignment in JavaScript like this: var a, b = "one", "two"; which would be equivalent to this: var a = "one"; var b = "two";
mlibre
  • 2,460
  • 3
  • 23
  • 32
15
votes
3 answers

Multiple assignment confusion

I understand that the assignment operator is right associative. So for example x = y = z = 2 is equivalent to (x = (y = (z = 2))) That being the case, I tried the following: foo.x = foo = {a:1} I expected that the object foo would be created with…
Danield
  • 121,619
  • 37
  • 226
  • 255
10
votes
3 answers

Python3 multiple assignment and memory address

After reading this and this, which are pretty similar to my question, I still cannot understand the following behaviour: a = 257 b = 257 print(a is b) #False a, b = 257, 257 print(a is b) #True When printing id(a) and id(b) I can see that the…
istern
  • 363
  • 1
  • 4
  • 13
6
votes
2 answers

Multiple assignment in R?

In python you can do something like this: x,y = 1,2 or x,y = function_that_returns_two_elements() I was trying to do something similar in R but found no solution. Is it possible?
Brzoskwinia
  • 371
  • 2
  • 11
6
votes
1 answer

Where is PowerShell's special array head/tail assignment documented?

I only just noticed that this is valid PowerShell code: PS> $first, $rest = @(1, 2, 3) This statement puts the first item in the array in $first and the remaining items in $rest. PS> $first 1 PS> $rest 2 3 It even works for an arbitrary number of…
4
votes
0 answers

Changing variable order in Python comma-separated multiple assignment

When one uses a multiple assignment in Python, such as a, b, c = b, c, a I've always thought that the relative order of the arguments is irrelevant (as long as one is consistent on both sides), i.e., the result would be the same if one does c, a,…
p-value
  • 608
  • 8
  • 22
3
votes
2 answers

LUA: howto omit a value of a multi-value in a multiple-assignment

If I want only the first and the third value of the function f(), I can do the following: local a, _, b = f(); Since _ is a valid name, maybe _ gets assigned a large table. Is there a way to omit this assignent to _ in the above case? (Clearly: if…
wimalopaan
  • 4,838
  • 1
  • 21
  • 39
2
votes
4 answers

Element swapping in list using one line multiple assignment

I am trying to swap 2 elements in a list. I want to swap the element at index 0 to the element at index arr[0], so basically I want to swap arr[0] <-> arr[arr[0]]. I have always used this method to swap elements between indexes i and j: arr[i],…
gkaz
  • 21
  • 4
2
votes
1 answer

reverse a linked list with multiple assignment - not all assignment orders work?

exactly the same as Reversing a LinkedList in with multiple assignment but if you try: pre, node, node.next = node, node.next, pre it does not work! (getting NoneType has no attribute next) both: pre, node.next, node = node, pre, node.next and…
ihadanny
  • 4,377
  • 7
  • 45
  • 76
2
votes
3 answers

Does C support multiple assignment of the type a , b = 0, 1 like python does?

""" Sample python code representing multiple assignment """ a , b = 0 , 1 print a , b The following code gives output : 0 1 and obviously does not raise any error. Does C support the same?
chtnnh
  • 25
  • 1
  • 8
2
votes
2 answers

Assign multiple returns of a function to the attributes of instances in a list

I have a list of class instances Child and I want to modify the toy attribute of each child using the list of toys returned from a function There is a working solution below but I am wondering if there is a one-liner? import random class Child(): …
2
votes
0 answers

multiple 2 thread 3-assignment using three compare and set objects

I'm working my way through the book 'The Art of Multiprocessor Programming' and am stuck on a certain question. The question as well as a proposed solution can be found here (Question 71) My issue is that their solution does not seem correct to me.…
1
vote
1 answer

How does a C++ command with two equal signs work?

I found some code in the program I work with: PWSTR myWchar = NULL; WCHAR *p = myWchar = new WCHAR[4]; How would I read a line with two equal signs? How is it computed? A: myWchar = new WCHAR[4]; WCHAR *p = myWchar or B: WCHAR *p = myWchar ; …
user3443063
  • 1,455
  • 4
  • 23
  • 37
1
vote
2 answers

python multiple assignment doesn't seem to occur at once

class ListNode: def __init__(self,val,next=None) -> None: self.val=val self.next=next n3=ListNode(3,n4) n2=ListNode(2,n3) n1=ListNode(1,n2) n2,n2.next,n1=n2.next,n1,n2 if it happen at once, n2.next should be n1, but result…
Silence
  • 11
  • 1
1
2 3