0

I'm am trying to assign values to multiple variables in one statement, but I cannot figure out if there is a nice syntax to split it across multiple lines.

# This is the long version I don't want
a, b, c, d = tf.constant(1, name='constant_a'), tf.constant(2, name='constant_b'), tf.constant(3, name='constant_c'), tf.constant(4, name='constant_d')

# Does something like this exist?
a, b, c, d = tf.constant(1, name='constant_a'), /
             tf.constant(2, name='constant_b'), /
             tf.constant(3, name='constant_c'), /
             tf.constant(4, name='constant_d')

Is there a nice, Pythonic way to do this?

Alan
  • 1,746
  • 7
  • 21
  • 2
    What is your motivation for that? (except curiosity?) What is the problem with simply: ```python a = tf.constant(..) ... d = tf.constant(..) ``` ? – pierresegonne Apr 13 '20 at 14:22

3 Answers3

1

Wrap what you have in parenthesis.

a, b, c, d = (tf.constant(1, name='constant_a'), 
              tf.constant(2, name='constant_b'), 
              tf.constant(3, name='constant_c'),
              tf.constant(4, name='constant_d'))
  • might as well use square brackets since they stand out more from the parentheses of the function calls, and there's no real difference between a list and a tuple in this context. BTW welcome to SO! – wjandrea Apr 13 '20 at 14:27
  • 1
    You're right that using square brackets looks a bit more Pythonic as it were. I chose the tuple because I found that it is ~1.3x faster than using a list in this context, although I need to think on why... The calculation time is so small for both it _really_ doesn't matter though. Thanks for the welcome! I'm happy to finally contribute as I'm a long-time beneficiary of the site. – Joseph O'Donnell Apr 13 '20 at 15:02
  • I could have sworn I read that lists behave as well as tuples in one-off unpackings like this, but I can't find it anymore. But yeah it makes sense that tuples perform better. Also I just noticed the parens around the names, `(a, b, c, d)` -- those aren't necessary. – wjandrea Apr 13 '20 at 15:11
  • You're right about the unnecessary parenthesis. I updated the answer. – Joseph O'Donnell Apr 13 '20 at 16:02
1

Not sure if it's more readable/Pythonic, but this is the most succinct!

a, b, c, d = [tf.constant(i, name='constant_' + x) for i, x in zip(range(1, 5), 'abcd')]
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Josh Friedlander
  • 10,870
  • 5
  • 35
  • 75
1

I think you were thinking of backslash (line continuation).

a, b, c, d = tf.constant(1, name='constant_a'), \
             tf.constant(2, name='constant_b'), \
             tf.constant(3, name='constant_c'), \
             tf.constant(4, name='constant_d')

This works, but it's ugly. Better to use a tuple/list like in Joseph's answer, or better yet a comprehension like in Josh's answer.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 1
    Yes I was! Thanks, I could have sworn that syntax existed but I couldn't figure out why it wouldn't work. I happen to agree it's ugly, I just wanted confirmation the line continuation syntax worked in assigning multiple variables. Silly mistake in the end, cheers for the answer! – Alan Apr 13 '20 at 14:54