0

I am starting maintenance on some code that assigns values to variables in this manner:

[thing_filenames, thing_list]=Load_Things(bunch_of_things)

I haven't seen values assigned to variables with brackets on the left side. Can someone please provide a pointer for where I can start to understand this statement?

Prayson W. Daniel
  • 14,191
  • 4
  • 51
  • 57
  • Did you look at https://docs.python.org/3/reference/simple_stmts.html#assignment-statements? – jonrsharpe Nov 18 '19 at 19:29
  • Load_Things looks like a function, so I'd look at def Load_Things(): and see what it returns. – mauve Nov 18 '19 at 19:30
  • From the doc: 'Assignment of an object to a target list, **optionally enclosed in parentheses or square brackets**, [...]' – Thierry Lathuille Nov 18 '19 at 19:32
  • The brackets can actually be omitted, but this is an example of *iterable unpacking*. I've gone ahead and closed this as a duplicate, but the accepted answer to that duplicate target will give you a ton of information about this and similar syntactical constructs in Python. – juanpa.arrivillaga Nov 18 '19 at 19:33
  • thanks; I understand now that I was asking an extremely basic question – Suzanne Brown Jan 06 '20 at 15:32

2 Answers2

1

It is normal unpacking of variables


[cat, dog] = ('Meow', 'Wuf')

print(cat) # 'Meow'
print(dog) # 'Wuf'

Load_Things(bunch_of_things) is a class or function that returns results that can be unpacked as I shown above.

Prayson W. Daniel
  • 14,191
  • 4
  • 51
  • 57
0

The brackets are only there to represent a group of variables. An equally valid line of code would be:

thing_filenames, thing_list = Load_Things(bunch_of_things)

The function Load_Things() most likely returns two values, which are then assigned to those two variables on one single line. Diving into Load_Things() will give you a better idea of what those variables represent in the code.

For example:

[item1, item2] = 1, 2
item3, item4 = 3, 4

print(item1, item2, item3, item4)
# Output: 1 2 3 4
SourPatchAdult
  • 179
  • 2
  • 9