0

a user is trying to update their info. I have been reading this line of code but I still do not understand what does the bio in front of the if statement does.

the varaible bio is actually from an input

bio = data.get('bio', '')

user.bio = bio if bio else user.bio

P.S.

I just realised, could this be like a shorthand for if bio exist return bio else return user.bio? Then in this way, if user try to empty their input field, it actually wouldn't work?

Tsuna
  • 2,098
  • 6
  • 24
  • 46
  • 1
    @MoinuddinQuadri ah! thx thx not used to see `ternary operator` this way. something new learned – Tsuna Dec 25 '16 at 21:50
  • BTW your these two line could be replaced with `user.bio = data.get('bio', user.bio)` and you will get the same result – Moinuddin Quadri Dec 25 '16 at 21:52
  • @MoinuddinQuadri by the way, really sorry for the duplicate. Think I should delete the post? When I first see the if else, I kept on thinking of if else staement and searched online about python if else...totally didn't think it's a ternary operator, thx for the replacing opinion :D didn't write the code but trying to understand it – Tsuna Dec 25 '16 at 21:53

1 Answers1

2

This means that ˋbio ˋ should be assigned to ˋuser.bio ˋ if the expression after the if is true. If the expression after the if return false, assign the result of the expression after else to ˋuser.bio ˋ.

It is the python equivalent of the C and C++ ?: ternary operator expression.

chmike
  • 20,922
  • 21
  • 83
  • 106