2

I have a one liner if statement that looks like this:

var = var if var < 1. else 1.

The first part var = var looks a bit ugly and I'd bet there's a more pythonic way to say this.

8
  • Not ugly to me, makes perfect readable sense. This assignment syntax is pretty pythonic. Aug 8, 2013 at 22:06
  • 2
    That is actually the pythonic way of doing it. Aug 8, 2013 at 22:06
  • 2
    How about flipping it? var = 1 if var > 1. else var
    – mr2ert
    Aug 8, 2013 at 22:06
  • 2
    Have a look at stackoverflow.com/questions/394809/…, they arrive at the same conclusion. Aug 8, 2013 at 22:07
  • 1
    @Smac89 the dot indicates that this is a float, not an integer.
    – Gabriel
    Aug 9, 2013 at 12:32

5 Answers 5

18

The following is 39% shorter and in my opinion is simpler and more pythonic than other answers. But we should note that sometimes people get it wrong thinking that 1 is a lower bound being confused by min function when actually 1 is an upper bound for var.

var = min(var, 1.0)
6
  • 4
    Despite your doubts, I think your answer is better and more simple than the asker's original code. Sure, neither is exactly complicated, but this is marginally easier for my brain to parse than the ternary expression is - I'd certainly rather see this one in the middle of some dense, difficult code than the original.
    – Mark Amery
    Aug 8, 2013 at 22:11
  • 4
    I think it is quite pythonic - it clearly states intent without having to look through extra logic.
    – tdelaney
    Aug 8, 2013 at 22:12
  • 2
    The OP is comparing to 1., not 1. This can potentially make a difference depending on the type of var.
    – arshajii
    Aug 8, 2013 at 22:17
  • 2
    +1, except that most readers will involuntary tend to associate the word min with a lower bound for var, when the real meaning is just the opposite. Seen this too many times. For readability, I prefer an if-based version even if slightly longer, and I wish language designers had promoted alternative names for min and max. Again, there is no need to make things potentially confusing if there is little or no gain. Aug 8, 2013 at 22:46
  • 1
    @sasha.sochka Yes, a lot about personal preference here. This's why my +1 from the beginning (I couldn't find an option "+0.9999999999" :) ). Aug 10, 2013 at 0:16
9
if var >= 1.:
    var = 1

or if you like one liners

if var >= 1.: var = 1
2
  • This is the best approach in my opinion, +1.
    – arshajii
    Aug 8, 2013 at 22:19
  • +1 It's also more readable by the occassional person more used to another programming language. I know, he/she will most probably get lost at other points of the program, but why contribute to confusion when there is no gain but "pythonicity"? Aug 8, 2013 at 22:28
5

Doesn't eliminate the var = var but it's shorter and one could argue, more pythonic:

var = min(var, 1.0)
0
4

Maybe this way would be shorter

var = min(var, 1.0)
2

You can use the equivalence of True=1 and False=0 to index into a 2-tuple of the possible values:

var = (1,var)[var < 1.]

If var < 1., then this evalutes to True, which is equivalent to 1. This simplifies to:

var = (1,var)[1]

Or

var = var

if not var < 1., this evaluates to False, which is equivalent to 0, giving:

var = (1,var)[0]

or

var = 1

So this one-liner:

var = (1,var)[var < 1.]

is equivalent to:

if var < 1.:
    var = var
else:
    var = 1
7
  • O.O I'm not sure I even understand this line.
    – Gabriel
    Aug 8, 2013 at 23:58
  • 1
    Interesting construction, but I don't consider relying on False = 0, True = 1 very pythonic... Aug 9, 2013 at 12:37
  • +1 For originality and applicability in other cases. Personally, I wouldn't use it because of the reasons exposed by @Gabriel. Aug 10, 2013 at 0:19
  • @Tobias Keinzler IMHO, the relationship is completely pythonic (in the same way as it is an integral part of C). Otherwise, the original language design would have included a separate Boolean type explicitly. Aug 10, 2013 at 0:48
  • 1
    @Tobias Keinzler Right, especially when None is equivalent to C's null and C's null also evaluates to False. I'm new to Python and realizing that "Pythonic" doesn't mean "supported by the Python language" but "preferred by the Python community/language spirit". In that case, I'm extremely glad these Boolean equivalencies / implicit conversions / definition of Python's boolean operators (I don't know how to exactly say it) are not considered "Pythonic". Aug 10, 2013 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.