6

I understood simple concepts about comma operator in python. For instance,

x0, sigma = 0, 0.1

means x0=0, and sigma=0.1. But I obtained a code that has a line that looks like the following.

y, xe = np.histogram(np.random.normal(x0, sigma, 1000))

where the output of y and xe are the followings.

y
Out[10]: array([  3,  17,  58, 136, 216, 258, 189,  87,  31,   5], dtype=int64)

xe
Out[11]: 
array([-0.33771565, -0.27400243, -0.21028922, -0.146576  , -0.08286279,
       -0.01914957,  0.04456364,  0.10827686,  0.17199007,  0.23570329,
        0.2994165 ])

I am not sure how to read y, xe expression. What could I look up to understand what it's saying?

user2357112
  • 260,549
  • 28
  • 431
  • 505
user7852656
  • 675
  • 4
  • 15
  • 26
  • Answerers, please be aware that the question is not about the `x0, sigma = 0, 0.1` line. It's about the `y, xe = np.histogram(np.random.normal(x0, sigma, 1000))` line. – user2357112 Aug 08 '17 at 17:44

3 Answers3

12

x0, sigma = 0, 0.1 is syntactic sugar. Some stuff is happening behind the scenes:

  • 0, 0.1 implicitly creates a tuple of two elements.

  • x0, sigma = unpacks that tuple into those two variables.

If you look at the docs for numpy.histogram, you see that it returns these two things:

hist : array
The values of the histogram. See density and weights for a description of the possible semantics.

bin_edges : array of dtype float
Return the bin edges (length(hist)+1).

Your y, xe = ... unpacks the tuple of the two returned arrays respectively. That is why your y is assigned to a numpy int64 array and your xe assigned to a numpy float array.

JoshuaRLi
  • 1,665
  • 14
  • 23
  • 3
    Actually `x0, sigma = 0, 0.1` doesn't even create the tuples - that's one of the few cases where Python optimizes the code (and avoids creating the tuples). However Python "could" create the tuples and that it doesn't is an "implementation detail" if I remember correctly. – MSeifert Aug 08 '17 at 18:44
  • 1
    @MSeifert Yeah, I could imagine something like that would be optimized by removing the overhead of creating a new tuple only if it is going to be assigned via unpacking. Never looked too deep into that though :P – JoshuaRLi Aug 08 '17 at 19:06
11

A comma forms a tuple, which in Python looks just like an immutable list.

Python does destructuring assignment, found in a few other languages, e.g. modern JavaScript. In short, a single assignment can map several left-hand variables to the same number of right-hand values:

foo, bar = 1, 2

This is equivalent to foo = 1 and bar = 2 done in one go. This can be used to swap values:

a, b = b, a

You can use a tuple or a list on the right side, and it will be unpacked (destructured) the same way if the length matches:

a, b = [1, 2]
# same effect as above:
xs = [1, 2]
a, b = xs
# again, same effect using a tuple:
ys = 1, 2
a, b = ys

You can return a tuple or a list from a function, and destructure the result right away:

def foo():
  return (1, 2, 3)  # Parens just add readability

a, b, c = foo()  # a = 1; b = 2; c = 3

I hope this answers your question. The histogram function returns a 2-tuple which is unpacked.

9000
  • 39,899
  • 9
  • 66
  • 104
1

This might be a solution for you:

def func():
    return 'a', 3, (1,2,3)  # returns a tuple of 3 elements (str, int, tuple)

x1, x2, x3 = func()  # unpacks the tuple of 3 elements into 3 vars
# x1: 'a'
# x2: 3
# x3: (1,2,3)
Fabien
  • 4,862
  • 2
  • 19
  • 33
sascha
  • 32,238
  • 6
  • 68
  • 110