python ? (conditional/ternary) operator for assignments

This question already has an answer here:

  • Does Python have a ternary conditional operator? 21 answers

  • Python has such an operator:

    variable = something if condition else something_else
    

    Alternatively, although not recommended (see @karadoc's comment):

    variable = (condition and something) or something_else
    

    In older Python code, you may see the trick:

    condition and something or something_else
    

    however, this has been superseded by the vastly superior ... if ... else ... construct:

    something if condition else something_else
    
    链接地址: http://www.djcxy.com/p/7390.html

    上一篇: 相当于`a?b:c`

    下一篇: python? (条件/三元)操作员