The mapping of this function to the IF statement does not work the same as the IF expression

This is a question for a home problem that I cannot understand:

Question Start

Q3. Try writing a function that does the same thing as an if statement:

def if_function(condition, true_result, false_result): """Return true_result if condition is a true value, and false_result otherwise.""" if condition: return true_result else: return false_result 

This function does not actually do the same as the if statement in all cases. To prove this fact, write the functions c, t and f so that one of these functions returns the number 1, and the other:

 def with_if_statement(): if c(): return t() else: return f() def with_if_function(): return if_function(c(), t(), f()) 

End question

Here is what I understood:

with_if_statement () does not evaluate f () if c () is true, but with_if_function () evaluates all 3 before checking if c () is true or not.

So, I was thinking about assigning a global variable in c () and changing its value in f ()

heres my code (which doesn't work):

 def c(): try: global x except NameError: x=1 if x==1: return True else: return False def t(): if x==1: return (1) else: return (0) def f(): global x x=2 if x==1: return (1) else: return (0) 

Can someone help me figure out the answer? Thanks!..

+4
source share
2 answers

The global operator should not raise NameError (and therefore you will not run x=1 in c() ). I would try to rewrite the code without using exceptions, they will not be needed to solve this problem and make it more complicated than it should be. Using a global variable and having side effects in your functions is definitely the right way.

+2
source
 def if_function(condition, true_result, false_result): """Return true_result if condition is a true value, and false_result otherwise. >>> if_function(True, 2, 3) 2 >>> if_function(False, 2, 3) 3 >>> if_function(3==2, 3+2, 3-2) 1 >>> if_function(3>2, 3+2, 3-2) 5 """ if condition: return true_result else: return false_result def with_if_statement(): """ >>> with_if_statement() 1 """ if c(): return t() else: return f() def with_if_function(): return if_function(c(), t(), f()) 

To do this, you must write three functions: c , t and f such that with_if_statement returns 1 , and with_if_function does not return 1 (and can do anything else)

In the beginning, the problem seems ridiculous, since logically with_if_statement and with_if_function same. However, if we see these two functions from the view of the interpreter, they are different.

The with_if_function function uses a call expression that ensures that all of its subexpressions of the operands are evaluated before the if_function is applied to the received arguments. Therefore, even if c returns False , the t function is called. In contrast, with_if_statement will never call t if c returns False (this paragraph from the UCB site).

 def c(): return True def t(): return 1 def f(): '1'.sort() # anything breaks the program is OK 
+2
source

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


All Articles