3

I was wondering how to take advantage of shorthand notation of if-else and += in Python of this simple expression:

I tried to set brackets everywhere and changed += to *= which didn't change the situation of course.

This works as expected:

a, b = 0, True
for i in range(123):
     if b == True:
          a = a + 1

Still working as expected, trying shorthand of if-else led me to:

a, b = 0, True
for i in range(123):
     a = a + 1 if b == True else a

Finally the attempt to write:

a, b = 0, True
for i in range(123):
     a += 1 if b == True else a:

fails and surprisingly I get pretty quickly huge integers for a

Moreover I'd really like something more shorthanded, e.g.:

a, b = 0, True
for i in range(123):
     a += 1 if b

The for-loop needs to stay as it is, since in my case there are other operations that affect b.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437

5 Answers5

4

Since noone seems to be posting, why it goes like this, here is mine - lines:

a = a + 1 if b == True else a
a += 1 if b == True else a

are seen by python as:

a = (a + 1 if b == True else a)
a += (1 if b == True else a)

This is why you get large numbers fast in second version - you will add a to a, when b is False. If you want to keep the if, then go:

a += (1 if b else 0)

Also don't compare b to True (or False), go foif b`, as it's more pythonic (it will prevent some weird mistakes, when other code will start to interact with yours).

EDIT: go for @Tomerikoo answer for even shorter code, but keep in mind, that those waters can be muddy and not everyone knows / easily follows, that adding boolean to int treats first as 1 (or 0 if False).

Radosław Cybulski
  • 2,952
  • 10
  • 21
  • ty for this extensive answer, since i want to assure that b is a boolean I consider comparing it with a boolean is the best way, am i wrong? – shorthand_fan Jul 19 '19 at 08:05
  • booleans are not *converted* to ints, they simply *are*. Look at my answer for examples. @shorthand_fan, you are not wrong in your approach, it is just not pythonic, let's say, to write `b == True` as simply checking for `b` will do. There are other ways for assuring `b` is bool but what you did is not wrong. I would however change to `b is True` – Tomerikoo Jul 19 '19 at 13:15
  • python's `boolean` is it's own type, therefore i'd say it's not integer. I'd have to dig thru python's source code, but i'm willing to hazard a guess, that there's a shortcut (or maybe straight `__mul__` operator, that when taken with int and bool does the magic. – Radosław Cybulski Jul 19 '19 at 13:18
  • yep, exactly what I meant. as you can see in my answer, bools are actually sub-class of int, where `True` has the value of 1 and `False` has the value of 0. So no conversion is actually needed and, as you said, the dunder functions take care of it by implementation. Read [this answer](https://stackoverflow.com/a/2764099/6045800) for more detailed (interesting) explanation – Tomerikoo Jul 19 '19 at 13:22
3

To closest to your proal is probably:

a, b = 0, True
for i in range(123):
   a += b

Since bool is a subtype of int, no conversion is necessary.

guidot
  • 5,095
  • 2
  • 25
  • 37
2

You can do:

for i in range(123):
     if b:a+=1
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
2

You can also do:

for i in range(123):
    a = a + 1*b

Because booleans are ints:

>>> isinstance(True, int)
True
>>> True == 1
True
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • While this is clever (i didn't knew i can multiply boolean with int and get away in python and i use python for like 10 years) this is also hard to read. `1 * b` is pretty much `b` to everyone and someone will sooner or later try to refactor the code by removing multiplication. – Radosław Cybulski Jul 19 '19 at 07:06
  • 1
    True, also can be shorthanded to `+=`. My intention was to show how the condition is still there and to make it more general. Say, if tomorrow OP will want to add `c` to `a` if `b` is True (so it will be `c*b`) – Tomerikoo Jul 19 '19 at 07:13
1

just note that

a += x if condition else y

will resolve to a += x if the condition is True; otherwise it will be a += y. this is why your numbers get big...

apart from that i suggest you use what U10-Forward's answer suggests.

hiro protagonist
  • 44,693
  • 14
  • 86
  • 111