0

I've just started learning Python. I saw an interesting code from here that the author used to explain short-circuiting. The code is as follows:

>>> def fun(i):
...     print "executed"
...     return i
... 

I tried to call fun(1). The output is the following and it makes perfect sense to me.

>>> fun(1)
executed
1

Then I tried [fun(i) for i in [0, 1, 2, 3]], and the output looked like this:

>>> [fun(i) for i in [0, 1, 2, 3]]
executed
executed
executed
executed
[0, 1, 2, 3]

I was expecting something like this:

executed
0
executed
1
executed
2
executed
3

Can anyone tell me what I got wrong? Thank you!

  • `[0, 1, 2, 3]` is the result of the list comprehension `[.. for .. in ..]`, which is output when that has completed. In the process of creating that list, a bunch of `print` statements were executed first. – deceze May 31 '21 at 09:21
  • FWIW, none of this has anything to do with *short circuiting*. – deceze May 31 '21 at 09:22
  • You can learn more about this topic from [www.certswarrior.com/exam/c2010-024/](http://www.certswarrior.com/exam/c2010-024/) and [www.certswarrior.com/exam/c2010-024/](http://www.certswarrior.com/exam/c2010-024/) course material. – Tamsin Zara May 31 '21 at 09:22
  • Short-circuiting is using boolean results to avoid further execution (no need to execute more on a `or` call if you already have a True result). I don't know what you expected, but I guess you missed a call to `any` somewhere. I advise to read back the explanation on the other question :) – aveuiller May 31 '21 at 09:23
  • 3
    FWIW, you shouldn't learn *Python 2* in this day and age, it's been dead for a decade. Learn Python 3. – deceze May 31 '21 at 09:23

3 Answers3

3
>>> def fun(i):
...     print "executed"
...     return i
... 

This is not example of short-circuiting. This is function with side effect - it does write to standard output, which is observable effect besides returning a value.

[fun(i) for i in [0, 1, 2, 3]]

This is list comprehension. Generally speaking you should have good reason to use function with side effect in such place, PEP 202 explains motivation for list comprehension as follows:

List comprehensions provide a more concise way to create lists in situations where map() and filter() and/or nested loops would currently be used.

Someone reading your code therefore will probably assume it has to just create list, without printing elements during this action.

Daweo
  • 31,313
  • 3
  • 12
  • 25
1

Square brakets in this context form a new list, which each element is function fun of according element in [0, 1, 2, 3]. It is called list list-comprehensions. "executed" is printed because during formation of new list function fun is called(for each element in [0, 1, 2, 3]). numbers 0,1,2 and 3 are not printed, because return value of fun is not printed, but put in the new list. To see the new list:

print("new list:",[fun(i) for i in [0, 1, 2, 3]])
user2952903
  • 365
  • 2
  • 10
0

Try doing

l = [fun(i) for i in [0, 1, 2, 3]]

This will output

executed
executed
executed
executed

Then just execute l, this will display the value of l, that is to say: [0, 1, 2, 3]

So when you are executing

>>> [fun(i) for i in [0, 1, 2, 3]]

This calls fun(0), fun(1), and so on, and displays executed, then it displays the computed value: [0, 1, 2, 3]

Be Chiller Too
  • 2,502
  • 2
  • 16
  • 42