Macaulay2 » Documentation
Packages » Python » PythonObject » PythonObject and PythonObject
next | previous | forward | backward | up | index | toc

# PythonObject and PythonObject -- logical conjunction of Python objects

- Operator: and
- Usage:
    `x and y`
- Inputs:
  - x, a python object,
  - y, a python object,
- Outputs:
  - a python object, logical conjunction of x and y;

## Description

Perform the logical conjunction ("and") operation on Python objects.

    i1 : toPython true and toPython true

    o1 = True

    o1 : PythonObject of class bool

If one of the arguments is a Macaulay2 object, then it is first converted to a Python object.

    i2 : toPython true and false

    o2 = false

    i3 : false and toPython true

    o3 = false

Unlike Macaulay2, Python supports using `and` with non-boolean objects. In this case, the first argument is returned if it is "falsy". Otherwise, the second argument is returned.

    i4 : toPython 0 and toPython "foo"

    o4 = 0

    o4 : PythonObject of class int

    i5 : toPython 5 and toPython "bar"

    o5 = bar

    o5 : PythonObject of class str

In Python, when the first argument is falsy, then is is returned immediately without evaluating the second argument. This is known as "short-circuiting". However, in Macaulay2, both arguments are evaluated before the Python conjunction method is called.

    i6 : stopIfError = false

    o6 = false

    i7 : toPython 0 and 1/0
    stdio:7:16:(3): error: division by zero

However, if the first argument is the Macaulay2 false object, then short-cicuiting will occur.

    i8 : false and pythonValue "1/0"

    o8 = false

## See also

- PythonObject or PythonObject -- logical disjunction of Python objects
- PythonObject xor PythonObject -- logical exclusive disjunction of Python objects
- not PythonObject -- logical negation of a python object
- PythonObject & PythonObject -- bitwise conjunction of Python objects

## Ways to use this method:

- PythonObject and PythonObject -- logical conjunction of Python objects
- `PythonObject and Thing`
- `Thing and PythonObject`

The source of this document is in /build/macaulay2-88fgJW/macaulay2-1.25.11+ds/M2/Macaulay2/packages/Python/doc/logical.m2:56:0.