+ 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.
24 Réponses
+ 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
+ 2
Now I see you want to accept and work with any type, not bool in particular.
Good luck trying 👍
+ 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.
+ 2
Oh that __and__ overload didn't work because it was overriding & (the bitwise and), instead of logical 'and'. Great...
+ 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
+ 2
Ipang that Infix thing is amazing, now all I've to do is understand it then see how I could do it here.
+ 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
+ 1
You mean __and__ magic overload is not an option, or it is, but there should be no `and` in its implementation?
+ 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))
+ 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.
+ 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
+ 1
Yes but, how to fix the overload? it's not using the overload as you'd expect
+ 1
That didn't pass tests, eg.:
A=7
B=8
assert (A and B) == (not(not A or not B))
+ 1
a=5
b=4
print((a!=0)*b) #same as (a and b)
Output:
4
Another one:
print(bool(a)*b)
+ 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.
+ 1
Okay bro
Let me know how it goes I wanna know also ...
0
You could use these asserts to test I guess: https://code.sololearn.com/cPZGkB6yyETc/?ref=app
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.
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?
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?