0

I'm currently doing tutorial for django-rest-framework.
I've noticed something weird in it, I'm not sure if it is specific for Django or whole Python. Please look at code below:

linenos = self.linenos and 'table' or False

It looks like shortened version of conditional instruction, but what is name of that operation? I can't find any specific information about it.

lineos is a boolean variable.

2 Answers 2

3

Read it like

linenos = (self.linenos and 'table') or False

Which returns 'table' if self.linenos is trueish, and False otherwise, due to the way that and and or are evaluated.

In modern Python we would write that using the ternary expression as

linenos = 'table' if self.linenos else False
Sign up to request clarification or add additional context in comments.

Comments

0

It is tri conditional flow
If self.linenos is true it assigns table to linenos or else False
we can also chain this flow for multiple conditions
This is just like var=condition1?12:3 in most other languages

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.