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?

1
  • 2
    What is your motivation for that? (except curiosity?) What is the problem with simply: python a = tf.constant(..) ... d = tf.constant(..) ? Commented Apr 13, 2020 at 14:22

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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!
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')]

Comments

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'))

4 Comments

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!
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.
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.
You're right about the unnecessary parenthesis. I updated the answer.

Your Answer

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.