Python assignment with AND and OR

1k views Asked by At

I am not much familiar with python. and I am write now looking into one code from python and it says something like this:

query = url.query and ('?' + url.query) or ''

can anyone help me understand what this means. I found something similar here. but I couldn't interpret the above statement. I am suppose to convert this line in Java.

1

There are 1 answers

2
Daniel Roseman On BEST ANSWER

That is very old - and quite unreliable - syntax for a ternary if. In modern Python it should be:

query = '?' + url.query if url.query else ''

and in Java:

query = url.query == '' ? '' : '?' + url.query