How to do "and" and "or" work when they are combined in one application?

For some reason, this function confused me:

def protocol(port): return port == "443" and "https://" or "http://" 

Can someone explain the order of what is going on behind the scenes to make this work the way it does.

I understood it this way until I tried:

Or A)

 def protocol(port): if port == "443": if bool("https://"): return True elif bool("http://"): return True return False 

Or b)

 def protocol(port): if port == "443": return True + "https://" else: return True + "http://" 

Is this some special case in Python, or am I completely misunderstanding how the instructions work?

+4
source share
7 answers

This is an old idiom; insert parentheses to display priority,

 (port == "443" and "https://") or "http://" 

x and y returns y if x is true, x if x is false; a or b , on the contrary, returns a if it is right, otherwise b .

So, if port == "443" true, this returns RHS and , i.e. "https://" . Otherwise, and is false, so or goes into the game and returns `` http: // '', its RHS.

In modern Python, the best way to translate this old idiom is:

 "https://" if port == "443" else "http://" 
+21
source

and returns the right operand if the left value is true. or returns the right operand if the left value is false. Otherwise, both return the left operand. They say that it unites .

+8
source

Is C and X or Y an early attempt at Python proxy users for C ? X : Y C ? X : Y

For the most part, it works, except if X is False - this led to many errors in the Python code, so in the Python FAQ , you will find a more correct solution (C and [X] or [Y])[0] , because the list with one element, regardless of its evaluated boolean, always True ! For example: [None] - True , but None - not. The OP example described above works because the line representing X is not empty.

However, all of this changed in Python 2.5 when a ternary or conditional statement was added to the language, which allows you to use the X if C else Y cleaner, as mentioned in other posts here. If you see code using an older format, this is because the user has long been a Python programmer who has not yet adopted the new syntax, they cut out n-paste another old code, or their employer still uses 2.4.x (or more earlier versions) etc.

+5
source

This is an ugly hack that is not recommended. It works due to a short circuit in the behavior of and and or and that they return one of their arguments, not a boolean value. Using this method poses a risk of introducing hard-to-reach errors, so do not use it in new code.

Here is an example of how the idiom and/or can give an unexpected result:

 >>> foo = 'foobar' >>> bar = 'foobar' >>> x = 0 >>> y = 1 >>> (foo == bar) and x or y # Will this return the value of x if (foo == bar)? 1 

Prefer a newer notation instead:

 return "https://" if port == "443" else "http://" 
+2
source

You can read the "and / or trick" of Python in this article, "Special Nature and / or in Python . " This is a bit like IIF() in VBA or VB, or ?: In C-style languages.

+1
source

This design works because it is "deployed" by the following code:

a and b →

 if a: return b else: return a 

a or b →

 if a: return a else: return b 
0
source

With all the good answers, I found that these statements help me remember this better and match the way my brain works (and hopefully a little more for it):

  • "and" returns the first False element (for example, None, "", [], (), {}, 0) or the last element if not (for example, False was not found)

    / li>
  • "or" returns the first True element or the last element (for example, True was not found) **

In conclusion :

  • they all return the first element that decides the outcome of the statement. (In the worst case, the last element in the sequence)

Please note that this rule also applies to chained all "and" or all "or" operators

0
source

Source: https://habr.com/ru/post/1306042/


All Articles