One line if-condition-assignment

前端 未结 14 2563
挽巷
挽巷 2020-12-02 08:46

I have the following code

num1 = 10
someBoolValue = True

I need to set the value of num1 to 20 if someBoolV

14条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-02 09:23

    If you wish to invoke a method if some boolean is true, you can put else None to terminate the trinary.

    >>> a=1
    >>> print(a) if a==1 else None
    1
    >>> print(a) if a==2 else None
    >>> a=2
    >>> print(a) if a==2 else None
    2
    >>> print(a) if a==1 else None
    >>>
    

提交回复
热议问题