steveo at syslang
Apr 20, 2012, 2:34 PM
Post #17 of 18
(1086 views)
Permalink

Re: can I overload operators like "=>", ">" or something like that?
[In reply to]


On 4/19/2012 3:28 PM, dmitrey wrote: > hi all, > can I somehow overload operators like "=>", ">" or something like > that? (I'm searching for appropriate overload for logical implication > "if a then b") > Thank you in advance, D. This tickled a memory from decades back when I worked in PL/I. They have a bool builtin function that performed bitwise boolean operations. The first two args were bit strings. The third arg is 4 bits whose value denoted the logical operation to be performed on the first two args on a bit by bit basis. The meaning of the four bits are 1st bit of arg3 defines the result of bool if A = '0'B and B = '0'B 2nd bit of arg3 defines the result of bool if A = '0'B and B = '1'B 3rd bit of arg3 defines the result of bool if A = '1'B and B = '0'B 4th bit of arg3 defines the result of bool if A = '1'B and B = '1'B bool ( A , B , '0001'B ) A AND B logical AND bool ( A , B , '0111'B ) A OR B logical OR bool ( A , B , '0110'B ) A XOR B exclusiveOR bool ( A , B , '1110'B ) A NAND B NOT AND bool ( A , B , '1000'B ) A NOR B NOT OR bool ( A , B , '1001'B ) A IFF B equivalence bool ( A , B , '1101'B ) A > B implication: if A then B bool ( A , B , '1011'B ) A < B implication: if B then A If A and B are just one bit (like True or False) then the operation basically becomes logical instead of bitwise. The full explanation is over at http://tinyurl.com/855dzjm Also more detail is here http://tinyurl.com/cdhwgdj So, if you *really* wanted to, you could define a bunch of constants like BOP_AND, BOP_OR, BOP_XOR, BOP_NAND, BOP_NOR, BOP_IFF, BOP_IMP and BOP_NIMP. Then define a class called Bool that redefines things like __rlshift__ and __rrshift__. That would get >>= and <<= for Implications and nodus tolens. It's not a total solution. I can't see how you're going to get IFF, NAND and NOR. But, maybe writing a bool function might be enough. If you implement bool in python then you can say things like def condition(A, B, bop): return bool(A, B, bop) A = something_boolish_thats_long_and_complex B = something_boolish_thats_long_and_more_complex bop = give_me_a_boolean_condition(*xx) boolVal = do_on_condition() if bool(A, B, bop) else None I think this might be useful to the right person where you have to calculate the logical operation at runtime. Passing in a constant operator is probably less useful. Is this horrible?  Time flies like the wind. Fruit flies like a banana. Stranger things have .0. happened but none stranger than this. Does your driver's license say Organ ..0 Donor?Black holes are where God divided by zero. Listen to me! We are all 000 individuals! What if this weren't a hypothetical question? steveo at syslang.net  http://mail.python.org/mailman/listinfo/pythonlist
