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 newExpr
object which represents the result of comparison between two expressions.Example
Let
x
andy
be instances ofExpr
, then>>> x = Variable(1, 'x') >>> y = Variable(1, 'y') >>> c = (x == y)
is also an instance of
Expr
. To evaluate and get its value, calleval()
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
- 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 ofeval()
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.
- __neg__()¶
- __add__(y)¶
- __sub__(y)¶
- __mul__(y)¶
- __truediv__(y)¶
- __floordiv__(y)¶
- __pow__(y)¶