-1

I am very new to python. On one of the tutorial I saw (about Tensorflow) I encourted this wierd notation that I never saw on any other langage.

So can someone explain what this do?

train_labels, train_samples = shuffle(train_labels, train_samples)

or

What does x, y = function(x, y) mean in general.

Tralamy
  • 3
  • 2
  • `function` returns a `tuple` of 2 objects - which is then immediately unwrapped into 2 variables `x` & `y`. It's called `unpacking`: https://realpython.com/lessons/tuple-assignment-packing-unpacking/ – rdas May 29 '21 at 13:09
  • In the case of `x, y = function(x, y)`, `function()` returns two variables that will be assigned to `x` and `y` respectively. It's perhaps a confusing example because the two function parameters are also named `x` and `y`, but those have nothing to do with it. – sandertjuh May 29 '21 at 13:09
  • In your case, `shuffle` seems to return a collection (like a list, or tuple), or something that can be iterated over - whatever it is, it seems to have two items in it, which are then assigned to `train_labels` and `train_samples` respectively. – Paul M. May 29 '21 at 13:10

2 Answers2

2

x, y = function(x, y) is for tuple unpacking. When the function you call returns 2 or more values it will assign the first one to x and the second value to y and so on. See the below example

def function():
   return 3, 4

x, y = function()

# x will be set to 3
# y will be set to 4
OmO Walker
  • 611
  • 6
  • 13
0

Ok so basically if the function() is defined like below:

def function():
    # Do some stubs
    return (1,2) # Or even return 1,2

Then it will return (1,2) as a tuple

And then if you do x, y = function(), it called tuple unpacking. Basically x is set to the first element of the tuple (1) and y is set to the second one (2).

If the function return more, something like return (4,5,6), you can just use x,y,z = function()

raspiduino
  • 601
  • 7
  • 16