What meaning is "if k in [0, len(n_trials) - 1] else None"?

Chris Angelico rosuav at gmail.com
Sun Dec 4 14:36:58 EST 2016


On Mon, Dec 5, 2016 at 6:14 AM, Terry Reedy <tjreedy at udel.edu> wrote:
> On 12/3/2016 7:31 PM, Chris Angelico wrote:
>>
>> On Sun, Dec 4, 2016 at 11:10 AM, Terry Reedy <tjreedy at udel.edu> wrote:
>>>>
>>>> But the expression result isn't even used. So this is better written:
>>>
>>>
>>> matplotlib.pyplot.xlabel sets x-axis scaling, with no documented return
>>> value.
>>> http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.xlabel
>>> If, as seems reasonable to assume, it returns None, the value of the
>>> expression is 'None if x else None', which is to say, None.
>>
>>
>> Hardly matters what the return value is, given that the code ignores it.
>
>
> I was not clear enough.  To me, a 'conditional expression' that has an
> unconditional value is a bit confusing.  So I think the conditional
> side-effect would be better written the way you did regardless of the use or
> not of the value.  If the unconditional value were not ignored, I think it
> would be better used by itself.

Fair enough. It's doubly bizarre - the expression result is constant
AND it's an expression being used as a statement that has an
equivalent statement form. Plus, the statement form takes two lines...
but the expression is over two lines anyway.

ChrisA


More information about the Python-list mailing list