2

I accidentally discovered this a=b=c=d=e=f=2 in python(2.7)(and JavaScript a few minutes later) interpreter .

Is this a feature or just the way the interpreter works, if is a feature how it is called ?

Do other languages have this feature ?

asked Sep 20, 2012 at 15:16
8
  • Please include code example. Commented Sep 20, 2012 at 15:26
  • a=b=c=d=e=f=2 is the code example you just run it in the interpreter either in python or browser console. Commented Sep 20, 2012 at 15:33
  • 2
    I can't think of a language that doesn't support chained assignment... It's existed seemingly forever. Commented Sep 20, 2012 at 18:05
  • @Izkata Both Pascal and Basic my starting languages didn't support that, maybe you started with C. Commented Sep 20, 2012 at 18:20
  • 1
    In C -- int a, b, c = 1; only sets c to 1. a and b are uninitialized. Commented Sep 24, 2012 at 18:26

4 Answers 4

12

In many languages, the = operator returns the value that was assigned.

The line a=b=c=d=e=f=2 is the same as a = (b = ( c = (d = (e = (f = 2)))))

f=2 returns the value of 2.

Thus, this then reduces to a = (b = ( c = (d = (e = 2)))) and so on.

This is known as chained assignment

answered Sep 20, 2012 at 18:28
4

(削除) This type of feature is typically known as "multiple assignment." Many languages have this type of feature. (削除ここまで)

This type of feature is typically known as "chained assignment." Languages that consider assignment to be expressions have this type of feature.

Multiple assignment typically means something else, e.g. Python:

a, b, c, d, e, f = [2, 2, 2, 2, 2, 2]

This type of syntax structure can be referred to as multiple assignment. In the context of Python, this is also known as unpacking.

The astute programmer might note that this is related to multiple return values:

def f(): return [1, 2, 3]
a, b, c = f()

There are a large number of languages that support chained assignment or multiple assignment in different ways.

C, C++, Java, and C# support the syntax you provided. VB does not support chained assignment (since assignment is a statement and not an expression like in the C family). Go supports multiple assignment similarly to Python, e.g. a swap:

a, b = b, a

I believe Lua supports multiple assignment and multiple return values. It can be done in a Lisp but there's usually a better way to go about it.

Basically, most languages support either of these features in some way.

answered Sep 20, 2012 at 15:33
4
  • I guess a bit of Pascal and Basic still lingers in my mind! Commented Sep 20, 2012 at 15:55
  • 2
    Actually, the version in the question is typically called "chained assignment". Commented Sep 20, 2012 at 18:03
  • 4
    multiple assignment (what's described here) is very different from what's being asked. The original question is about "assignment is an expression", which allows another assignment to use the same value. Commented Sep 20, 2012 at 19:19
  • You're right. I made appropriate changes to the answer. Commented Sep 24, 2012 at 17:51
2

Assignments like these are OK for simple objects. For others, it can lead to undesirable side effects. In linked list the example below, in the line:

last = last.nxt = Item(2)

We may expect that Item(1).nxt points to Item(2) and last points to Item(2) However, we end up with a circular reference where Item(2).nxt points to itself.

For example:

class Item(object):
 def __init__(self, value):
 self.value = value
 self.nxt = None
 def __str__(self):
 return 'self: id(self): %d, value: %s, nxt: %d' % (id(self), \
 str(self.value), id(self.nxt))
 prev = last = Item(1)
 print prev
 >>> self: id(self): 140382915976272, value: 1, nxt: 9568656
 print last
 >>> self: id(self): 140382915976272, value: 1, nxt: 9568656
 print id(None)
 >>> 9568656
 last = last.nxt = Item(2)
 print prev
 >>> self: id(self): 140382915976272, value: 1, nxt: 9568656
 print last
 >>> self: id(self): 140382915975120, value: 2, nxt: 140382915975120
answered Jun 9, 2014 at 20:12
1
  • I wanted to point out the subtle differences between different languages for statements that seemingly look similar. For example, the same type of assignment in C or Java sets up the linked list as expected. ie, item(1).nxt points to item(2) and item(2).nxt points to null. Commented Jun 10, 2014 at 19:29
1

It is a feature of the language, the = statement evaluates the statement to its right and assigns that value to the variable on the left.

In Python a statement like a, b = 1,2 is also legal. That is called multiple assignment. See Python Docs I don't know of a special term for the a=b=2 structure though. It is just multiple assignments on the same line.

answered Sep 20, 2012 at 15:31
3
  • 1
    This is wrong. This only works in languages where assignment is not a statement. If it were a statement, then it wouldn't have a value and couldn't be assigned. Commented Sep 21, 2012 at 2:39
  • sorry what in particular is wrong? Commented Sep 21, 2012 at 16:17
  • @ben336 I believe you have confused statement with expression. if(foo) {} and return 5; are statatements. On the other hand a + 5 and i++ are expressions. In pascal, a := 5; is a statement. In C, a = 5; is an expression. Its semantics, but it is important semantics when talking about languages. Commented Sep 24, 2012 at 19:12

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.