[Tutor] 'or' in assignment (not if statement)?

Adam Bark adam.jtm30 at gmail.com
Fri Dec 10 01:59:34 CET 2010


On 10/12/10 00:51, Alex Hall wrote:
> Hi all,
> I am reading the source of a project I hope to help with
> (http://www.qwitter-client.net). I sometimes see something like:
> val=val or 1
> I am guessing that val is an int. If val==0, the 'or' kicks in and
> val=1, else the or is not needed and val=val. Am I close? Can other
> words or symbols be used in contexts where one would not normally
> think of them?
> Thanks.
>
>    
Hi Alex,

This is one of those times the interactive interpreter comes in handy eg:

In [1]: val=5

In [2]: val=val or 1

In [3]: val
Out[3]: 5

In [4]: val=0

In [5]: val=val or 1

In [6]: val
Out[6]: 1

You are right by the way and I know you can't test every possibility 
but, as you already suspected the outcome, this just reinforces it I think.

HTH,
Adam.


More information about the Tutor mailing list