1

given a string "A & B & C" with values of A,B abd C = 0,0,1 respectively, it would be solved as 0 & 0 & 1 to give a result of 0.

I am writing a program where I want to check for & and | for logic operation to return the result. in this case, I wish to have something like this logic = and. But unfortunately and is a python reserved keyword and it will throw the error below;

File "<ipython-input-248-9d5aa5c6e082>", line 1
    logic = and
            ^
SyntaxError: invalid syntax

How do I fix this?

Dismas
  • 377
  • 1
  • 3
  • 10
  • Assuming you could do this, how do you expect it to work any different than using `and` directly? In other words, you'd just be storing a pointer to an operator, and still need to use the actual operator – OneCricketeer May 03 '21 at 13:07
  • Can you clarify what *behaviour* you expect? What should be contained inside ``logic``? How would one use it? The boolean operators ``and``/``or`` cannot be expressed by function calls, so "storing" them makes little sense – you cannot use them. – MisterMiyagi May 03 '21 at 13:11
  • Can you be more explicit about the use case? For example, can you give an example of a function that you'd like to be able to write if you knew the answer to this question? – Jon Kiparsky May 03 '21 at 13:11
  • 1
    I think they are looking to be able to parse `True logic False` where `logic` might be a variable assigned either `and` or `or`. Is that correct @dismas? – JonSG May 03 '21 at 13:17
  • Are you aware that Python has a ``&`` operator? This one can be stored in a variable, via ``operator.__add__``. – MisterMiyagi May 03 '21 at 13:40

3 Answers3

4

You can't use a keyword in this way. You might think that you could use the built-in module operator to achieve your goal. This module contains functions that have the same behaviour as the built-in operators, and you can store a reference to one of those functions.

However, logical and and or are missing from this module (and_ and or_ are bitwise). This is probably because these operators exhibit "short-circuit" behaviour, which can't be emulated in a function call.

If it's okay for you not to have short-circuiting, it's trivial to implement these functions yourself:

>>> and_ = lambda a, b: a and b
>>> logic = and_
>>> logic(True, False)
False
>>> logic(True, True)
True
Thomas
  • 174,939
  • 50
  • 355
  • 478
  • 1
    ``operator.and_`` is the bitwise ``&`` not the logical ``and``. – MisterMiyagi May 03 '21 at 13:34
  • @MisterMiyagi Well spotted! Do you know where logical `and` resides? The module contains logical `not_`, but I can't find the other logical operators... – Thomas May 03 '21 at 13:36
  • 2
    There is no logical ``and`` ``operator``. The ``and`` operator cannot be accurately expressed as a function – that would destroy its short-circuit behaviour. – MisterMiyagi May 03 '21 at 13:37
2

The difference between functions and operators in python is that functions are objects and operators are not. What this means is that and is not a "thing" in python that you can pass around the way you could pass around a value or a function or an object or a class.

You could pass around "the idea of and", by encapsulating it in a function:

and_logic = lambda x, y: a and b

This means that you'd now have a function which, for any a and b returns the logical result of a and b. This function could now be passed around in potentially useful or interesting ways.

But since you don't give a lot of information about what you want to do in your question, it's hard to be more specific about what you can do with this idea.

EDIT: Just after I submitted this, I learned something from @Thomas' post. Apparently, the work of creating the lambda has been done for you!

Jon Kiparsky
  • 7,499
  • 2
  • 23
  • 38
0

Yes, of course. You can do almost fancy things by using Python.

>>> import operator
>>> op = operator.or_
>>> print(op(1, 2))
3
An Nguyen
  • 96
  • 1
  • 2
  • 6