![]() |
is this a bug or not with if...else...? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: is this a bug or not with if...else...? (/thread-12607.html) |
is this a bug or not with if...else...? - meokey - Sep-02-2018 platform.python_version()'3.5.1' a=None; b='abc'; c = b if a == None else a + ' & ' + bTraceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' I thought that the rest part after 'else' would not be interpreted since a==None. Any thought please? Thank you. RE: is this a bug or not with if...else...? - Gribouillis - Sep-02-2018 If I copy and paste the same line of code in my python 3.5.2 interpreter, there is no error... RE: is this a bug or not with if...else...? - Larz60+ - Sep-02-2018 looks like it violates PEP 20, but runs. I don't see where the else is being interpreted as the 1st condition is satisfied. reversed will give syntax error, because forced to run else code of first statement a=None; b='abc'; a + ' & ' b if a != None else c = b
RE: is this a bug or not with if...else...? - Gribouillis - Sep-03-2018 @Larz60+ You forgot a + in the reversed version, and you cannot inverse the assignment statement. The reversed version isa=None; b='abc'; c = a + ' & ' + b if a != None else band it also works. RE: is this a bug or not with if...else...? - volcano63 - Sep-03-2018 Accepted (and more efficient) form of None value check is is /is not if a is None: ..... if b is not None: ..... |