How to make something that's equivalent to the and operator but without using and itself? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to make something that's equivalent to the and operator but without using and itself?

maybe a function, but to also respect the lazy eval of and. So a and b vs fand(a,b) or fand([a,b]) but get lazy eval too? Is it possible? Without lazy eval is easy, a one line function.

15th Feb 2022, 6:45 PM
newlyrefocused
24 Answers
+ 4
# If you want to use ’and’ to open up (evaluate) the second argument (short circut), you can use an if-else statement, as you already mentioned: >>> a, b = 3, 4 >>> print(a and b) 4 >>> print(a if not a else b) 4 >>> print(b if a else a) 4 >>> print(b if all((a, b)) else 0) 4 >>> print((lambda a, b: b if all((a, b)) else 0)(a, b)) 4
15th Feb 2022, 9:24 PM
Per Bratthammar
Per Bratthammar - avatar
+ 2
Now I see you want to accept and work with any type, not bool in particular. Good luck trying 👍
15th Feb 2022, 10:20 PM
Ipang
+ 2
Every value in Python is truthly or falsy. Truthly values can be evaluated to True and falsy values to False. For integers, the only falsy value is 0.
15th Feb 2022, 10:24 PM
Per Bratthammar
Per Bratthammar - avatar
+ 2
Oh that __and__ overload didn't work because it was overriding & (the bitwise and), instead of logical 'and'. Great...
15th Feb 2022, 11:04 PM
newlyrefocused
+ 2
NewlyRefocused, Sorry I was mistaken, it apparently is supposed to be for the bitwise AND operator overloading. I just found these recently, https://stackoverflow.com/questions/471546/any-way-to-override-the-and-operator-in-JUMP_LINK__&&__python__&&__JUMP_LINK https://stackoverflow.com/questions/32311518/is-it-possible-to-overload-logical-and-in-python
16th Feb 2022, 4:24 AM
Ipang
+ 2
Ipang that Infix thing is amazing, now all I've to do is understand it then see how I could do it here.
16th Feb 2022, 4:42 AM
newlyrefocused
+ 2
Ipang it looks nice to use like "50 |infix_and| 0"(ignore the dbl.quotes) but I still don't have the lazy eval. I'll try to understand it. (Infix stuff is at the end of this code blob): https://code.sololearn.com/cPZGkB6yyETc/?ref=app
16th Feb 2022, 5:08 AM
newlyrefocused
+ 1
You mean __and__ magic overload is not an option, or it is, but there should be no `and` in its implementation?
15th Feb 2022, 8:27 PM
Ipang
+ 1
# Hi! One way is to use all(): print(all((True, True)) == (True and True)) print(all((True, False)) == (True and False)) print(all((False, True)) == (False and True)) print(all((False, False)) == (False and False))
15th Feb 2022, 8:47 PM
Per Bratthammar
Per Bratthammar - avatar
+ 1
NewlyRefocused, Why return 9 at line 9? Also <value> argument in __init()__ needs to be casted as `bool` to ensure we only work with logical values.
15th Feb 2022, 10:08 PM
Ipang
+ 1
the return 9 is to see if that function is called, so the print should show 9 instead of [] Also remember that '7 and 8' returns 8 not True, hence why not forcing bool casting
15th Feb 2022, 10:09 PM
newlyrefocused
+ 1
Yes but, how to fix the overload? it's not using the overload as you'd expect
15th Feb 2022, 10:21 PM
newlyrefocused
+ 1
That didn't pass tests, eg.: A=7 B=8 assert (A and B) == (not(not A or not B))
16th Feb 2022, 12:42 AM
newlyrefocused
+ 1
a=5 b=4 print((a!=0)*b) #same as (a and b) Output: 4 Another one: print(bool(a)*b)
16th Feb 2022, 3:18 AM
Brian
Brian - avatar
+ 1
I'm interested in the "and" operator, instead of the "&" operator. Specifically this is how it works: "assert (a and b) == (b if a else a)" for any value or type of a,b I hope.
16th Feb 2022, 4:24 AM
newlyrefocused
+ 1
Okay bro Let me know how it goes I wanna know also ...
16th Feb 2022, 4:43 AM
Ipang
0
You could use these asserts to test I guess: https://code.sololearn.com/cPZGkB6yyETc/?ref=app
15th Feb 2022, 8:51 PM
newlyrefocused
0
I don't know what magic overload is. all() returns bool so it's no good eg 3 and 4 is 4 not True like all([3,4]) would be.
15th Feb 2022, 8:59 PM
newlyrefocused
0
yeah, that's pretty good like that, but I was hoping to compress it into one word so as to not repeat the args (in this case 'a') But I guess 'a fand b' cant be done in python?
15th Feb 2022, 9:32 PM
newlyrefocused
0
Someone said that maybe generators could be used for the lazy eval. I've not reached that far in my learning, does anyone know how and if that could work here?
15th Feb 2022, 9:33 PM
newlyrefocused