chainer.utils.type_check.Expr

class chainer.utils.type_check.Expr(priority)[source]

Abstract syntax tree of an expression.

It represents an abstract syntax tree, and isn’t a value. You can get its actual value with eval() function, and get syntax representation with the __str__() method. Each comparison operator (e.g. ==) generates a new Expr object which represents the result of comparison between two expressions.

Example

Let x and y be instances of Expr, then

>>> x = Variable(1, 'x')
>>> y = Variable(1, 'y')
>>> c = (x == y)

is also an instance of Expr. To evaluate and get its value, call eval() method:

>>> c.eval()
True

Call str function to get a representation of the original equation:

>>> str(c)
'x == y'

You can actually compare an expression with a value:

>>> (x == 1).eval()
True

Note that you can’t use boolean operators such as and, as they try to cast expressions to boolean values:

>>> z = Variable(1, 'z')
>>> x == y and y == z  # raises an error
Traceback (most recent call last):
RuntimeError: Don't convert Expr to bool. Please call Expr.eval method to evaluate expression.

Methods

__call__(*args)[source]

Call self as a function.

__getitem__(key)[source]
eval()[source]

Evaluates the tree to get actual value.

Behavior of this function depends on an implementation class. For example, a binary operator + calls the __add__ function with the two results of eval() function.

__eq__(y)

Return self==value.

__ne__(y)

Return self!=value.

__lt__(y)

Return self<value.

__le__(y)

Return self<=value.

__gt__(y)

Return self>value.

__ge__(y)

Return self>=value.

__nonzero__()[source]
__bool__()[source]
__neg__()
__add__(y)
__radd__(y)[source]
__sub__(y)
__rsub__(y)[source]
__mul__(y)
__rmul__(y)[source]
__truediv__(y)
__rtruediv__(y)[source]
__floordiv__(y)
__rfloordiv__(y)[source]
__pow__(y)