0

I have something that I dont understand here. I mean, it works, but I would like to understand what is going on behind the scenes:

def load_data():
    f = gzip.open('mnist.pkl.gz', 'rb')
    training_data, validation_data, test_data = pickle.load(f, encoding="latin1")
    f.close()
    return (training_data, validation_data, test_data)

so, what I am trying to understand is here, in line 3, there are 3 variables(training_data, validation_data and test_data). These are being assigned to one function though!? My question is, what happens to them and/or the function? Is the function executed three times? Or are the variables simply a function then that can be called? Or is the loaded data being split in 3 parts, a third of them being distributed to every variable? I am a noob, so this is probably stupid, but please help me!

zvone
  • 18,045
  • 3
  • 49
  • 77
Max K.
  • 5
  • 4

3 Answers3

0

pickle.load(...) is executed once. The construct you see is called unpacking and is more or less equivalent to

result = pickle.load(f, encoding="latin1")
training_data = result[0]
validation_data = result[1]
test_data = result[2]

(actually unpacking does one more thing: it assures that result has exactly 3 elements) Does this help?

freakish
  • 54,167
  • 9
  • 132
  • 169
0

no the function is not performed three times, but returns values at the same time.

this question can help you.

escattoni
  • 1
  • 1
  • 1
0

This is called sequence unpacking. You can find it in Python documentation., where it says:

The statement t = 12345, 54321, 'hello!' is an example of tuple packing: the values 12345, 54321 and 'hello!' are packed together in a tuple. The reverse operation is also possible:

x, y, z = t

This is called, appropriately enough, sequence unpacking and works for any sequence on the right-hand side.

This feature of the language makes it easy to write functions which seem to return multiple results (although they actually just return a tuple), e.g.

def get_three_numbers():
    return 5, 7, 11

a, b, c = get_three_numbers()
zvone
  • 18,045
  • 3
  • 49
  • 77