Skip to content

Instantly share code, notes, and snippets.

@FrankApiyo
Last active June 19, 2021 13:45
Show Gist options
  • Save FrankApiyo/913547316123971ead52bf27d09fc4ef to your computer and use it in GitHub Desktop.
Save FrankApiyo/913547316123971ead52bf27d09fc4ef to your computer and use it in GitHub Desktop.
[Python]Simultaneous Assignment

When using simultaneous assignment, all the expressions on the RHS are evaluated before any assigment is done on the LHS.

Simultaneous assignment can greatly simplify the presentation of code.

fibonacci generator (no simultaneous assigment)

def fibonacci():
  a = 0
  b = 1
  while True:
    yield a
    future = a + b
    a = b
    b = future

fibonacci generator (with simultaneous assignment)

def fibonacci():
  a, b = 0, 1
  while True:
    yield a
    a, b = b, a + b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment