Rufus McGee
Rufus McGee

Reputation: 45

A pythonic (3) way to declare more than one variable on a line

I ran across some code in GitHub https://github.com/OeslleLucena/FASNet

It threw a syntax error at this line:

# dimensions of images. (less than 224x 224)
img_width, img_height = (,)

It looks like the code is trying to declare multiple variables on the same line. I see them get passed as parameters later on. I am assuming this code worked at some point but I haven't seen this convention used before. Is it a python 2 thing? Are they empty tuples? How would you do this properly in Python 3? TIA

Upvotes: 1

Views: 154

Answers (2)

BlueSheepToken
BlueSheepToken

Reputation: 6159

In addition to Moberg's answer, in Python3 you can declare an element and a list in one line with the operator *.

head, *queue = range(5)

It initializes two variables head and queue with values 0 and [1, 2, 3, 4] respectively.

Upvotes: 2

Moberg
Moberg

Reputation: 5469

This kind of code is a syntax error in both Python2 and Python3. Perhaps the code is meant to be changed? For example

a, b = (10, 20)

initializes two variables a and b with values 10 and 20 respectively.

Upvotes: 8

Related Questions