Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Assignment (computer science)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Chained assignment=== A statement like <code>w = x = y = z</code> is called a '''chained assignment''' in which the value of <code>z</code> is assigned to multiple variables <code>w, x,</code> and <code>y</code>. Chained assignments are often used to initialize multiple variables, as in <code>a = b = c = d = f = 0</code> Not all programming languages support chained assignment. Chained assignments are equivalent to a sequence of assignments, but the evaluation strategy differs between languages. For simple chained assignments, like initializing multiple variables, the evaluation strategy does not matter, but if the targets (l-values) in the assignment are connected in some way, the evaluation strategy affects the result. In some programming languages ([[C (programming language)|C]] for example), chained assignments are supported because assignments are expressions, and have values. In this case chain assignment can be implemented by having a [[Operator associativity#Right-associativity of assignment operators|right-associative assignment]], and assignments happen right-to-left. For example, <code>i = arr[i] = f()</code> is equivalent to <code>arr[i] = f(); i = arr[i]</code>. In [[C++]] they are also available for values of class types by declaring the appropriate return type for the assignment operator. In [[Python (programming language)|Python]], assignment statements are not expressions and thus do not have a value. Instead, chained assignments are a series of statements with multiple targets for a single expression. The assignments are executed left-to-right so that <code>i = arr[i] = f()</code> evaluates the expression <code>f()</code>, then assigns the result to the leftmost target, <code>i</code>, and then assigns the same result to the next target, <code>arr[i]</code>, using the new value of <code>i</code>.<ref>{{cite web|url=https://docs.python.org/reference/simple_stmts.html#assignment-statements|title=7. Simple statements β Python 3.6.5 documentation|website=docs.python.org|access-date=20 April 2018}}</ref> This is essentially equivalent to <code>tmp = f(); i = tmp; arr[i] = tmp</code> though no actual variable is produced for the temporary value.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)