# Variable

relationalai.semantics.frontend.base

Represents a composable value in the semantics DSL.

A `Variable` is the base type for most objects you use when building
queries and definitions. Variables are *placeholders* that can be
combined into larger expressions via Python operators and then passed to
fragment/model methods like [`Model.where`](/api/python/v2.3/semantics/frontend/base/model_class/index.md#method-where) and [`Model.select`](/api/python/v2.3/semantics/frontend/base/model_class/index.md#method-select).

Common subclasses include [`Concept`](/api/python/v2.3/semantics/frontend/base/concept_class/index.md) (entity types), [`Relationship`](/api/python/v2.3/semantics/frontend/base/relationship_class/index.md),
[`Ref`](/api/python/v2.3/semantics/frontend/base/ref_class/index.md) (named entity references created by [`Concept.ref`](/api/python/v2.3/semantics/frontend/base/concept_class/index.md#method-ref)),
[`Chain`](/api/python/v2.3/semantics/frontend/base/chain_class/index.md) (property/relationship access like `Person.age`), and
[`Expression`](/api/python/v2.3/semantics/frontend/base/expression_class/index.md) (relationship calls and composed arithmetic/comparison expressions).

## Parameters

- `model` ([`Model`](/api/python/v2.3/semantics/frontend/base/model_class/index.md)) - The semantic model this variable belongs to.

## Examples

Build a filter with variable comparisons and select an aliased column:

```python
from relationalai.semantics import Integer, Model, String

m = Model()
Person = m.Concept("Person")
m.define(
    Person.new(id=1, name="Alice", age=30),
    Person.new(id=2, name="Bob", age=17),
)
m.where(Person.age >= 18).select(Person.name.alias("NAME")).to_df()
```

## Notes

Most users should not instantiate this class directly.
You typically obtain variables by:

- Creating concepts via [`Model.Concept`](/api/python/v2.3/semantics/frontend/base/model_class/index.md#method-concept).
- Accessing relationships as attributes (e.g. `Person.age`).
- Creating reference variables via [`Concept.ref`](/api/python/v2.3/semantics/frontend/base/concept_class/index.md#method-ref).

Variables overload several Python operators to build DSL expressions:

- Arithmetic operators like `+` and `*` return an
  [`Expression`](/api/python/v2.3/semantics/frontend/base/expression_class/index.md).
- Comparison operators like `==` and `>=` return an expression suitable
  for use in `where`/`require` clauses.
- Use `&` and `|` to combine conditions; do not use Python's `and`/`or`.
- For relationships/properties/chains (e.g. `Person.age`), operators apply
  to the **output field** (the last field). To work with a **non-output** field,
  index the relationship/property/chain to get a [`FieldRef`](/api/python/v2.3/semantics/frontend/base/fieldref_class/index.md), e.g.
  `Person.jobs["start_date"]`.

`Variable` objects cannot be used in Python boolean contexts (`if` /
`while`), and they cannot be iterated or passed to `len()`; these usages
raise `RAIException` with a helpful message.

## Methods

### .`__add__`()

```python
Variable.__add__(other)
```

Return an addition expression (`self + other`).

This enables Python's `+` operator for semantics DSL variables.

**Parameters:**

- `other` ([`Value`](/api/python/v2.3/semantics/frontend/base/value/index.md)) - The right-hand operand.

**Returns:**

- [`Expression`](/api/python/v2.3/semantics/frontend/base/expression_class/index.md) - A composable addition expression.

**Raises:**

- `relationalai.util.error.RAIException` - If `other` is an invalid value in an expression (for example, a
  table schema).
- `NotImplementedError` - If a Python value cannot be converted to a supported literal.

### .`__radd__`()

```python
Variable.__radd__(other)
```

Return an addition expression (`other + self`).

This is the reflected form of [`Variable.__add__`](/api/python/v2.3/semantics/frontend/base/variable_class/index.md#method-__add__). It enables
expressions like `1 + Person.age`.

**Parameters:**

- `other` ([`Value`](/api/python/v2.3/semantics/frontend/base/value/index.md)) - The left-hand operand.

**Returns:**

- [`Expression`](/api/python/v2.3/semantics/frontend/base/expression_class/index.md) - A composable addition expression.

**Raises:**

- `relationalai.util.error.RAIException` - If `other` is an invalid value in an expression (for example, a
  table schema).
- `NotImplementedError` - If a Python value cannot be converted to a supported literal.

### .`__mul__`()

```python
Variable.__mul__(other)
```

Return a multiplication expression (`self * other`).

This enables Python's `*` operator for semantics DSL variables.

**Parameters:**

- `other` ([`Value`](/api/python/v2.3/semantics/frontend/base/value/index.md)) - The right-hand operand.

**Returns:**

- [`Expression`](/api/python/v2.3/semantics/frontend/base/expression_class/index.md) - A composable multiplication expression.

**Raises:**

- `relationalai.util.error.RAIException` - If `other` is an invalid value in an expression (for example, a
  table schema).
- `NotImplementedError` - If a Python value cannot be converted to a supported literal.

### .`__rmul__`()

```python
Variable.__rmul__(other)
```

Return a multiplication expression (`other * self`).

This is the reflected form of [`Variable.__mul__`](/api/python/v2.3/semantics/frontend/base/variable_class/index.md#method-__mul__). It enables
expressions like `365 * Person.age`.

**Parameters:**

- `other` ([`Value`](/api/python/v2.3/semantics/frontend/base/value/index.md)) - The left-hand operand.

**Returns:**

- [`Expression`](/api/python/v2.3/semantics/frontend/base/expression_class/index.md) - A composable multiplication expression.

**Raises:**

- `relationalai.util.error.RAIException` - If `other` is an invalid value in an expression (for example, a
  table schema).
- `NotImplementedError` - If a Python value cannot be converted to a supported literal.

### .`__sub__`()

```python
Variable.__sub__(other)
```

Return a subtraction expression (`self - other`).

This enables Python's `-` operator for semantics DSL variables.

**Parameters:**

- `other` ([`Value`](/api/python/v2.3/semantics/frontend/base/value/index.md)) - The right-hand operand.

**Returns:**

- [`Expression`](/api/python/v2.3/semantics/frontend/base/expression_class/index.md) - A composable subtraction expression.

**Raises:**

- `relationalai.util.error.RAIException` - If `other` is an invalid value in an expression (for example, a
  table schema).
- `NotImplementedError` - If a Python value cannot be converted to a supported literal.

### .`__rsub__`()

```python
Variable.__rsub__(other)
```

Return a subtraction expression (`other - self`).

This is the reflected form of [`Variable.__sub__`](/api/python/v2.3/semantics/frontend/base/variable_class/index.md#method-__sub__). It enables
expressions like `5 - Person.age`.

**Parameters:**

- `other` ([`Value`](/api/python/v2.3/semantics/frontend/base/value/index.md)) - The left-hand operand.

**Returns:**

- [`Expression`](/api/python/v2.3/semantics/frontend/base/expression_class/index.md) - A composable subtraction expression.

**Raises:**

- `relationalai.util.error.RAIException` - If `other` is an invalid value in an expression (for example, a
  table schema).
- `NotImplementedError` - If a Python value cannot be converted to a supported literal.

### .`__truediv__`()

```python
Variable.__truediv__(other)
```

Return a division expression (`self / other`).

This enables Python's `/` operator for semantics DSL variables.

**Parameters:**

- `other` ([`Value`](/api/python/v2.3/semantics/frontend/base/value/index.md)) - The right-hand operand.

**Returns:**

- [`Expression`](/api/python/v2.3/semantics/frontend/base/expression_class/index.md) - A composable division expression.

**Raises:**

- `relationalai.util.error.RAIException` - If `other` is an invalid value in an expression (for example, a
  table schema).
- `NotImplementedError` - If a Python value cannot be converted to a supported literal.

### .`__rtruediv__`()

```python
Variable.__rtruediv__(other)
```

Return a division expression (`other / self`).

This is the reflected form of [`Variable.__truediv__`](/api/python/v2.3/semantics/frontend/base/variable_class/index.md#method-__truediv__). It enables
expressions like `1 / Person.age`.

**Parameters:**

- `other` ([`Value`](/api/python/v2.3/semantics/frontend/base/value/index.md)) - The left-hand operand.

**Returns:**

- [`Expression`](/api/python/v2.3/semantics/frontend/base/expression_class/index.md) - A composable division expression.

**Raises:**

- `relationalai.util.error.RAIException` - If `other` is an invalid value in an expression (for example, a
  table schema).
- `NotImplementedError` - If a Python value cannot be converted to a supported literal.

### .`__floordiv__`()

```python
Variable.__floordiv__(other)
```

Return a floor-division expression (`self // other`).

Enables Python's `//` operator for semantics DSL variables. The
quotient is floored toward negative infinity, matching Python: for
operands of opposite sign, `-7 // 2` is `-4` (not `-3`). The named
function form is `math.floor_divide`; use `math.trunc_divide` for
division that truncates toward zero instead.

**Parameters:**

- `other` ([`Value`](/api/python/v2.3/semantics/frontend/base/value/index.md)) - The right-hand operand.

**Returns:**

- [`Expression`](/api/python/v2.3/semantics/frontend/base/expression_class/index.md) - A composable floor-division expression.

**Raises:**

- `relationalai.util.error.RAIException` - If `other` is an invalid value in an expression (for example, a
  table schema).
- `NotImplementedError` - If a Python value cannot be converted to a supported literal.

### .`__rfloordiv__`()

```python
Variable.__rfloordiv__(other)
```

Return a floor-division expression (`other // self`).

Reflected form of [`Variable.__floordiv__`](/api/python/v2.3/semantics/frontend/base/variable_class/index.md#method-__floordiv__); the quotient is
floored toward negative infinity, matching Python (`math.trunc_divide`
truncates toward zero instead). Enables expressions like
`5 // Person.age`.

**Parameters:**

- `other` ([`Value`](/api/python/v2.3/semantics/frontend/base/value/index.md)) - The left-hand operand.

**Returns:**

- [`Expression`](/api/python/v2.3/semantics/frontend/base/expression_class/index.md) - A composable floor-division expression.

**Raises:**

- `relationalai.util.error.RAIException` - If `other` is an invalid value in an expression (for example, a
  table schema).
- `NotImplementedError` - If a Python value cannot be converted to a supported literal.

### .`__pow__`()

```python
Variable.__pow__(other)
```

Return an exponentiation expression (`self ** other`).

This enables Python's `**` operator for semantics DSL variables.

**Parameters:**

- `other` ([`Value`](/api/python/v2.3/semantics/frontend/base/value/index.md)) - The right-hand operand.

**Returns:**

- [`Expression`](/api/python/v2.3/semantics/frontend/base/expression_class/index.md) - A composable exponentiation expression.

**Raises:**

- `relationalai.util.error.RAIException` - If `other` is an invalid value in an expression (for example, a
  table schema).
- `NotImplementedError` - If a Python value cannot be converted to a supported literal.

### .`__rpow__`()

```python
Variable.__rpow__(other)
```

Return an exponentiation expression (`other ** self`).

This is the reflected form of [`Variable.__pow__`](/api/python/v2.3/semantics/frontend/base/variable_class/index.md#method-__pow__). It enables
expressions like `2 ** Person.age`.

**Parameters:**

- `other` ([`Value`](/api/python/v2.3/semantics/frontend/base/value/index.md)) - The left-hand operand.

**Returns:**

- [`Expression`](/api/python/v2.3/semantics/frontend/base/expression_class/index.md) - A composable exponentiation expression.

**Raises:**

- `relationalai.util.error.RAIException` - If `other` is an invalid value in an expression (for example, a
  table schema).
- `NotImplementedError` - If a Python value cannot be converted to a supported literal.

### .`__mod__`()

```python
Variable.__mod__(other)
```

Return a modulo expression (`self % other`).

Enables Python's `%` operator for semantics DSL variables. The result
follows Python (and floor division): it takes the sign of the divisor,
so `-7 % 2` is `1` (not `-1`). The named function form is
`math.modulo`; use `math.remainder` for the truncated remainder
(sign of the dividend) instead.

**Parameters:**

- `other` ([`Value`](/api/python/v2.3/semantics/frontend/base/value/index.md)) - The right-hand operand.

**Returns:**

- [`Expression`](/api/python/v2.3/semantics/frontend/base/expression_class/index.md) - A composable modulo expression.

**Raises:**

- `relationalai.util.error.RAIException` - If `other` is an invalid value in an expression (for example, a
  table schema).
- `NotImplementedError` - If a Python value cannot be converted to a supported literal.

### .`__rmod__`()

```python
Variable.__rmod__(other)
```

Return a modulo expression (`other % self`).

Reflected form of [`Variable.__mod__`](/api/python/v2.3/semantics/frontend/base/variable_class/index.md#method-__mod__); the result follows Python,
taking the sign of the divisor (`math.remainder` gives the truncated
remainder instead). Enables expressions like `10 % Person.age`.

**Parameters:**

- `other` ([`Value`](/api/python/v2.3/semantics/frontend/base/value/index.md)) - The left-hand operand.

**Returns:**

- [`Expression`](/api/python/v2.3/semantics/frontend/base/expression_class/index.md) - A composable modulo expression.

**Raises:**

- `relationalai.util.error.RAIException` - If `other` is an invalid value in an expression (for example, a
  table schema).
- `NotImplementedError` - If a Python value cannot be converted to a supported literal.

### .`__neg__`()

```python
Variable.__neg__()
```

Return a negation expression (`-self`).

This enables Python's unary `-` operator for semantics DSL variables.
The negation is represented as multiplication by `-1` in the DSL.

**Returns:**

- [`Expression`](/api/python/v2.3/semantics/frontend/base/expression_class/index.md) - A composable negation expression.

### .`__gt__`()

```python
Variable.__gt__(other)
```

Return a greater-than filter expression (`self > other`).

This enables Python's `>` operator for semantics DSL variables.
The returned expression is typically passed to [`Model.where`](/api/python/v2.3/semantics/frontend/base/model_class/index.md#method-where) or
[`Fragment.where`](/api/python/v2.3/semantics/frontend/base/fragment_class/index.md#method-where) to filter a query.

**Parameters:**

- `other` ([`Value`](/api/python/v2.3/semantics/frontend/base/value/index.md)) - The right-hand operand.

**Returns:**

- [`Expression`](/api/python/v2.3/semantics/frontend/base/expression_class/index.md) - A composable boolean expression suitable for `where`/`require`
  clauses.

**Raises:**

- `relationalai.util.error.RAIException` - If `other` is an invalid value in an expression (for example, a
  table schema).
- `NotImplementedError` - If a Python value cannot be converted to a supported literal.

### .`__ge__`()

```python
Variable.__ge__(other)
```

Return a greater-than-or-equal filter expression (`self >= other`).

This enables Python's `>=` operator for semantics DSL variables.
The returned expression is typically passed to [`Model.where`](/api/python/v2.3/semantics/frontend/base/model_class/index.md#method-where) or
[`Fragment.where`](/api/python/v2.3/semantics/frontend/base/fragment_class/index.md#method-where) to filter a query.

**Parameters:**

- `other` ([`Value`](/api/python/v2.3/semantics/frontend/base/value/index.md)) - The right-hand operand.

**Returns:**

- [`Expression`](/api/python/v2.3/semantics/frontend/base/expression_class/index.md) - A composable boolean expression suitable for `where`/`require`
  clauses.

**Raises:**

- `relationalai.util.error.RAIException` - If `other` is an invalid value in an expression (for example, a
  table schema).
- `NotImplementedError` - If a Python value cannot be converted to a supported literal.

### .`__lt__`()

```python
Variable.__lt__(other)
```

Return a less-than filter expression (`self < other`).

This enables Python's `<` operator for semantics DSL variables.
The returned expression is typically passed to [`Model.where`](/api/python/v2.3/semantics/frontend/base/model_class/index.md#method-where) or
[`Fragment.where`](/api/python/v2.3/semantics/frontend/base/fragment_class/index.md#method-where) to filter a query.

**Parameters:**

- `other` ([`Value`](/api/python/v2.3/semantics/frontend/base/value/index.md)) - The right-hand operand.

**Returns:**

- [`Expression`](/api/python/v2.3/semantics/frontend/base/expression_class/index.md) - A composable boolean expression suitable for `where`/`require`
  clauses.

**Raises:**

- `relationalai.util.error.RAIException` - If `other` is an invalid value in an expression (for example, a
  table schema).
- `NotImplementedError` - If a Python value cannot be converted to a supported literal.

### .`__le__`()

```python
Variable.__le__(other)
```

Return a less-than-or-equal filter expression (`self <= other`).

This enables Python's `<=` operator for semantics DSL variables.
The returned expression is typically passed to [`Model.where`](/api/python/v2.3/semantics/frontend/base/model_class/index.md#method-where) or
[`Fragment.where`](/api/python/v2.3/semantics/frontend/base/fragment_class/index.md#method-where) to filter a query.

**Parameters:**

- `other` ([`Value`](/api/python/v2.3/semantics/frontend/base/value/index.md)) - The right-hand operand.

**Returns:**

- [`Expression`](/api/python/v2.3/semantics/frontend/base/expression_class/index.md) - A composable boolean expression suitable for `where`/`require`
  clauses.

**Raises:**

- `relationalai.util.error.RAIException` - If `other` is an invalid value in an expression (for example, a
  table schema).
- `NotImplementedError` - If a Python value cannot be converted to a supported literal.

### .`__eq__`()

```python
Variable.__eq__(other) -> Any
```

Return an equality filter expression (`self == other`).

This enables Python's `==` operator for semantics DSL variables.
The returned expression is typically passed to [`Model.where`](/api/python/v2.3/semantics/frontend/base/model_class/index.md#method-where) or
[`Fragment.where`](/api/python/v2.3/semantics/frontend/base/fragment_class/index.md#method-where) to filter a query.

**Parameters:**

- `other` ([`Value`](/api/python/v2.3/semantics/frontend/base/value/index.md)) - The right-hand operand.

**Returns:**

- [`Expression`](/api/python/v2.3/semantics/frontend/base/expression_class/index.md) - A composable boolean expression suitable for `where`/`require`
  clauses.

**Raises:**

- `relationalai.util.error.RAIException` - If `other` is an invalid value in an expression (for example, a
  table schema).
- `NotImplementedError` - If a Python value cannot be converted to a supported literal.

### .`__ne__`()

```python
Variable.__ne__(other) -> Any
```

Return an inequality filter expression (`self != other`).

This enables Python's `!=` operator for semantics DSL variables.
The returned expression is typically passed to [`Model.where`](/api/python/v2.3/semantics/frontend/base/model_class/index.md#method-where) or
[`Fragment.where`](/api/python/v2.3/semantics/frontend/base/fragment_class/index.md#method-where) to filter a query.

**Parameters:**

- `other` ([`Value`](/api/python/v2.3/semantics/frontend/base/value/index.md)) - The right-hand operand.

**Returns:**

- [`Expression`](/api/python/v2.3/semantics/frontend/base/expression_class/index.md) - A composable boolean expression suitable for `where`/`require`
  clauses.

**Raises:**

- `relationalai.util.error.RAIException` - If `other` is an invalid value in an expression (for example, a
  table schema).
- `NotImplementedError` - If a Python value cannot be converted to a supported literal.

### .`__or__`()

```python
Variable.__or__(other) -> Match
```

Return a match expression (`self | other`).

This enables Python's `|` operator for semantics DSL values and
statements.

`a | b` builds a [`Match`](/api/python/v2.3/semantics/frontend/base/match_class/index.md) with two branches. At execution time,
the match picks the first branch (left-to-right) that can succeed for
the current bindings.

This makes `|` the right tool for *fallback/default* behavior ("use
this value if available, otherwise use that one"). If you instead want
to combine the results from multiple branches (set-style union), use
[`union`](/api/python/v2.3/semantics/union/index.md) or [`Model.union`](/api/python/v2.3/semantics/frontend/base/model_class/index.md#method-union).

**Parameters:**

- `other` ([`Statement`](/api/python/v2.3/semantics/frontend/base/statement/index.md)) - The right-hand branch.

**Returns:**

- [`Match`](/api/python/v2.3/semantics/frontend/base/match_class/index.md) - A composable match expression. You can extend it with additional
  branches via `|`.

**Raises:**

- `relationalai.util.error.RAIException` - Raised in the following cases:
  - Branches return different numbers of values (for example, mixing a
    value-producing branch with a multi-column select).

**Examples:**

Set a default value for a property that may be missing:

```python
from relationalai.semantics import Model

m = Model()
User = m.Concept("User")
m.define(
    User.new(username="alice", age=30, status="active"),
    User.new(username="bob", age=15, status="inactive"),
    User.new(username="carol", age=40),
)
# Get status with a default of "missing"
status = User.status | "missing"
m.select(User.username, status).to_df()
```

**Notes:**

Do not use Python's boolean `or` with DSL variables. Use `|`,
[`union`](/api/python/v2.3/semantics/union/index.md), or [`Model.union`](/api/python/v2.3/semantics/frontend/base/model_class/index.md#method-union) instead.

**Referenced By:**

1. [RelationalAI Documentation](/index.md)1) [Build With RelationalAI](/build/index.md)1. [Build a semantic model](/build/modeling/index.md)1) [Derive facts with logic](/build/modeling/derive-facts-with-logic/index.md)1. [Write conditional definitions with `Model.where` and `Model.define`](/build/modeling/derive-facts-with-logic/index.md#write-conditional-definitions-with-modelwhere-and-modeldefine)

### .`or_`()

```python
Variable.or_(other) -> Match
```

Return `self`, or `other` where `self` has no value (`self | other`).

`a.or_(b)` builds the same [`Match`](/api/python/v2.3/semantics/frontend/base/match_class/index.md) as `a | b`. As a method
call it binds tighter than any operator, so a sum of defaulted terms
needs no parentheses: `a.or_(0) + b.or_(0)`, whereas `a | 0 + b | 0`
parses as `a | (0 + b) | 0`. Calls chain like `|`:
`a.or_(b).or_(c)` is `a | b | c`.

Like `|`, this takes the first branch that succeeds for the current
bindings. To combine the values of all branches, use
[`union`](/api/python/v2.3/semantics/union/index.md).

An aggregate over no members has no value, so `or_` is how to make an
empty sum or count 0. Call `.per` and `.where` on the aggregate
before `.or_`.

**Parameters:**

- `other` ([`Statement`](/api/python/v2.3/semantics/frontend/base/statement/index.md)) - The fallback, used where `self` has no value.

**Returns:**

- [`Match`](/api/python/v2.3/semantics/frontend/base/match_class/index.md) - The match `self | other`.

**Examples:**

Total each department's salaries, with 0 for a department that has no
employees:

```python
from relationalai.semantics import Integer, Model, String
from relationalai.semantics.std import aggregates

m = Model()
Department = m.Concept("Department", identify_by={"name": String})
Employee = m.Concept("Employee", identify_by={"name": String})
Employee.department = m.Property(f"{Employee} works in {Department}")
Employee.salary = m.Property(f"{Employee} earns {Integer:salary}")
sales, research = Department.new(name="Sales"), Department.new(name="Research")
m.define(sales, research, Employee.new(name="Ann", department=sales, salary=100))
payroll = (
    aggregates.sum(Employee.salary)
    .per(Department)
    .where(Employee.department == Department)
    .or_(0)
)
m.select(Department.name, payroll).to_df()
```

### .`__and__`()

```python
Variable.__and__(other) -> Fragment
```

Return a fragment that combines conditions with AND (`self & other`).

Use `&` to explicitly conjoin conditions when building filters. This is
equivalent to passing multiple conditions to [`Model.where`](/api/python/v2.3/semantics/frontend/base/model_class/index.md#method-where).
If `other` is a [`Fragment`](/api/python/v2.3/semantics/frontend/base/fragment_class/index.md), this returns a new fragment with
`self` added to `other`'s filters (roughly `other.where(self)`).

**Parameters:**

- `other` (`Statement or Fragment`) - The right-hand operand. This is typically a condition expression, but it may also be an existing fragment to extend.

**Returns:**

- [`Fragment`](/api/python/v2.3/semantics/frontend/base/fragment_class/index.md) - A composable fragment representing the conjunction.

**Raises:**

- `relationalai.util.error.RAIException` - If `other` is not a valid statement in a filter.
- `NotImplementedError` - If a Python value cannot be converted to a supported literal.

**Examples:**

Extend an existing fragment by adding another condition, then continue
chaining:

```python
from relationalai.semantics import Model

m = Model()
User = m.Concept("User")
m.define(
    User.new(username="alice", age=30, status="active"),
    User.new(username="bob", age=15, status="inactive"),
    User.new(username="carol", age=40),
)
active_users = m.where(User.status == "active")
active_adults = active_users & (User.age >= 18)
active_adults.select(User.username).to_df()
```

**Notes:**

Do not use Python's boolean `and` with DSL variables. Use `&`
(or pass multiple conditions to `where`) instead.

**Referenced By:**

1. [RelationalAI Documentation](/index.md)1) [Build With RelationalAI](/build/index.md)1. [Build a semantic model](/build/modeling/index.md)1) [Derive facts with logic](/build/modeling/derive-facts-with-logic/index.md)1. [Write conditional definitions with `Model.where` and `Model.define`](/build/modeling/derive-facts-with-logic/index.md#write-conditional-definitions-with-modelwhere-and-modeldefine)

### .`and_`()

```python
Variable.and_(other) -> Fragment
```

Return a fragment that combines conditions with AND (`self & other`).

`a.and_(b)` builds the same [`Fragment`](/api/python/v2.3/semantics/frontend/base/fragment_class/index.md) as `a & b`, so
`m.where(a.and_(b))` filters like `m.where(a, b)`.

**Parameters:**

- `other` (`Statement or Fragment`) - The right-hand condition, or a fragment to extend.

**Returns:**

- [`Fragment`](/api/python/v2.3/semantics/frontend/base/fragment_class/index.md) - The fragment `self & other`.

### .`as_bool`()

```python
Variable.as_bool() -> AsBool
```

Return a boolean-typed wrapper around this variable.

`as_bool()` converts a DSL value/statement into a boolean expression
that can be selected or composed like any other value. At execution
time, the result is `True` when the wrapped expression can succeed
for the current bindings, and `False` otherwise.

This is useful for turning *presence* into a column (for example,
whether an optional property/relationship exists) and for explicitly
selecting conditions as data.

**Returns:**

- [`AsBool`](/api/python/v2.3/semantics/frontend/base/asbool_class/index.md) - A composable boolean expression.

**Examples:**

Select whether an optional property is present:

```python
from relationalai.semantics import Model

m = Model()
Person = m.Concept("Person")
m.define(
    Person.new(name="Alice", nickname="Al", age=30),
    Person.new(name="Bob", age=17),
)
has_nickname = Person.nickname.as_bool()
m.select(Person.name, has_nickname.alias("has_nickname")).to_df()
```

Select a condition as a column explicitly without using `as_bool()`:

```python
is_adult = Person.age >= 18
m.select(Person.name, is_adult.alias("is_adult")).to_df()
```

**Notes:**

- You cannot use the result in Python boolean contexts (`if` /
  `while`); it is only meaningful inside semantics DSL expressions.
- Comparisons and other filter-style expressions selected via
  [`Model.select`](/api/python/v2.3/semantics/frontend/base/model_class/index.md#method-select) are automatically treated as boolean outputs,
  so calling `as_bool()` is usually optional.

**Referenced By:**

1. [RelationalAI Documentation](/index.md)1) [Build With RelationalAI](/build/index.md)1. [Build a semantic model](/build/modeling/index.md)1) [Derive facts with logic](/build/modeling/derive-facts-with-logic/index.md)

### .`alias`()

```python
Variable.alias(alias: str) -> Alias
```

Return this value labeled with an output alias.

`alias()` wraps this value in an [`Alias`](/api/python/v2.3/semantics/frontend/base/alias_class/index.md). The alias is primarily
used to name the output column when you pass the value to [`Model.select`](/api/python/v2.3/semantics/frontend/base/model_class/index.md#method-select)
(and therefore affects column labels in [`Fragment.to_df`](/api/python/v2.3/semantics/frontend/base/fragment_class/index.md#method-to_df)).

When a selected value has an alias, the resulting fragment can also be
indexed by that name (for example `query["AGE"]`).

**Parameters:**

- `alias` (`str`) - The output name to attach to this value.

**Returns:**

- [`Alias`](/api/python/v2.3/semantics/frontend/base/alias_class/index.md) - A composable aliased value.

**Examples:**

Alias selected columns for readable output and later indexing:

```python
from relationalai.semantics import Model

m = Model()
Person = m.Concept("Person")
m.define(
    Person.new(name="Alice", age=30),
    Person.new(name="Bob", age=17),
)
m.select(Person.name.alias("person_name"), Person.age.alias("person_age")).to_df()
```

**Notes:**

`alias()` does not rename the underlying concept/relationship; it only
influences how the value is labeled in query outputs.

### .`in_`()

```python
Variable.in_(values: Sequence[Value] | Variable)
```

Return an SQL IN-style filter expression.

Use `in_` to test whether this value equals *any* of a set of
candidate values.

This is a DSL-friendly alternative to Python's `in` operator (which
cannot be overloaded for DSL variables). The method lowers to an
equality against a one-column value source:

- If `values` is a [`Variable`](/api/python/v2.3/semantics/frontend/base/variable_class/index.md), this is equivalent to `self == values`.
- If `values` is a sequence of Python primitives (`str`, `int`,
  `float`, `bool`), the method builds a one-column table via
  [`Model.data`](/api/python/v2.3/semantics/frontend/base/model_class/index.md#method-data) and compares against that column.
- Otherwise, the method unions the provided items via [`Model.union`](/api/python/v2.3/semantics/frontend/base/model_class/index.md#method-union)
  and compares against the resulting value.

**Parameters:**

- `values` (`Sequence[Value] or Variable`) - Candidate values to match.

**Returns:**

- [`Expression`](/api/python/v2.3/semantics/frontend/base/expression_class/index.md) - A composable boolean expression suitable for [`Model.where`](/api/python/v2.3/semantics/frontend/base/model_class/index.md#method-where)
  and [`Fragment.where`](/api/python/v2.3/semantics/frontend/base/fragment_class/index.md#method-where).

**Raises:**

- `relationalai.util.error.RAIException` - Raised in the following cases:
  - Any provided value is not valid in an expression (for example, a
    table schema).
  - The constructed union branches return different numbers of
    values.
- `NotImplementedError` - If a Python value cannot be converted to a supported literal.

**Examples:**

Filter rows by membership in a Python list:

```python
from relationalai.semantics import Model

m = Model()
Person = m.Concept("Person")
m.define(
    Person.new(name="Alice", age=10),
    Person.new(name="Bob", age=30),
    Person.new(name="David", age=50),
)
m.where(Person.name.in_(["Alice", "Bob"])).select(Person.name, Person.age).to_df()
```

Pass a variable (for example the result of [`Model.union`](/api/python/v2.3/semantics/frontend/base/model_class/index.md#method-union)):

```python
allowed = m.union("Alice", "Bob")
m.where(Person.name.in_(allowed)).select(Person.name).to_df()
```

**Referenced By:**

1. [RelationalAI Documentation](/index.md)1) [Interact with RelationalAI](/interact/index.md)1. [Query a model with PyRel](/interact/query-a-model-with-pyrel/index.md)1) [Filter results with `where`](/interact/query-a-model-with-pyrel/index.md#filter-results-with-where)

## Inheritance Hierarchy

```d2 inline layout=dagre theme=0 darkTheme=false pad=8 title="Inheritance hierarchy"
direction: down
"relationalai.semantics.frontend.base.Variable": {
  label: "Variable"
  link: "/api/python/v2.3/semantics/frontend/base/variable_class"
}
"relationalai.semantics.frontend.base.DSLBase": {
  label: "DSLBase"
  link: "/api/python/v2.3/semantics/frontend/base/dslbase_class"
}
"relationalai.semantics.frontend.base.DSLBase" -> "relationalai.semantics.frontend.base.Variable"
```

## Subclassed By

1. [semantics](/api/python/v2.3/semantics/index.md)1) [frontend](/api/python/v2.3/semantics/frontend/index.md)1. [base](/api/python/v2.3/semantics/frontend/base/index.md)1) [Aggregate](/api/python/v2.3/semantics/frontend/base/aggregate_class/index.md)
         2) [Alias](/api/python/v2.3/semantics/frontend/base/alias_class/index.md)
         3) [AsBool](/api/python/v2.3/semantics/frontend/base/asbool_class/index.md)
         4) [Chain](/api/python/v2.3/semantics/frontend/base/chain_class/index.md)
         5) [Concept](/api/python/v2.3/semantics/frontend/base/concept_class/index.md)
         6) [Data](/api/python/v2.3/semantics/frontend/base/data_class/index.md)
         7) [DerivedColumn](/api/python/v2.3/semantics/frontend/base/derivedcolumn_class/index.md)
         8) [DerivedTable](/api/python/v2.3/semantics/frontend/base/derivedtable_class/index.md)
         9) [Expression](/api/python/v2.3/semantics/frontend/base/expression_class/index.md)
         10) [FieldRef](/api/python/v2.3/semantics/frontend/base/fieldref_class/index.md)
         11) [FilterBy](/api/python/v2.3/semantics/frontend/base/filterby_class/index.md)
         12) [Fragment](/api/python/v2.3/semantics/frontend/base/fragment_class/index.md)
         13) [Literal](/api/python/v2.3/semantics/frontend/base/literal_class/index.md)
         14) [LookupBy](/api/python/v2.3/semantics/frontend/base/lookupby_class/index.md)
         15) [Match](/api/python/v2.3/semantics/frontend/base/match_class/index.md)
         16) [New](/api/python/v2.3/semantics/frontend/base/new_class/index.md)
         17) [NumberConcept](/api/python/v2.3/semantics/frontend/base/numberconcept_class/index.md)
         18) [Property](/api/python/v2.3/semantics/frontend/base/property_class/index.md)
         19) [Reading](/api/python/v2.3/semantics/frontend/base/reading_class/index.md)
         20) [Ref](/api/python/v2.3/semantics/frontend/base/ref_class/index.md)
         21) [Relationship](/api/python/v2.3/semantics/frontend/base/relationship_class/index.md)
         22) [Table](/api/python/v2.3/semantics/frontend/base/table_class/index.md)
         23) [Union](/api/python/v2.3/semantics/frontend/base/union_class/index.md)
      2. [core](/api/python/v2.3/semantics/frontend/core/index.md)1) [AnyNumber](/api/python/v2.3/semantics/frontend/core/anynumber_class/index.md)
   2) [std](/api/python/v2.3/semantics/std/index.md) / [re](/api/python/v2.3/semantics/std/re/index.md)1) [RegexMatch](/api/python/v2.3/semantics/std/re/regexmatch_class/index.md)

## Used By

1. [semantics](/api/python/v2.3/semantics/index.md)1) [frontend](/api/python/v2.3/semantics/frontend/index.md) / [base](/api/python/v2.3/semantics/frontend/base/index.md)1. [Alias](/api/python/v2.3/semantics/frontend/base/alias_class/index.md)
      2. [AsBool](/api/python/v2.3/semantics/frontend/base/asbool_class/index.md)
      3. [Concept](/api/python/v2.3/semantics/frontend/base/concept_class/index.md)1) [require](/api/python/v2.3/semantics/frontend/base/concept_class/index.md#method-require)
      4. [Variable](/api/python/v2.3/semantics/frontend/base/variable_class/index.md)1) [in\_](/api/python/v2.3/semantics/frontend/base/variable_class/index.md#method-in_)
      5. [Value](/api/python/v2.3/semantics/frontend/base/value/index.md)
   2) [inspect](/api/python/v2.3/semantics/inspect/index.md)1) [to\_concept](/api/python/v2.3/semantics/inspect/to_concept/index.md)
   3) [reasoners](/api/python/v2.3/semantics/reasoners/index.md) / [prescriptive](/api/python/v2.3/semantics/reasoners/prescriptive/index.md) / [problem](/api/python/v2.3/semantics/reasoners/prescriptive/problem/index.md)1) [Problem](/api/python/v2.3/semantics/reasoners/prescriptive/problem/problem_class/index.md)1. [maximize](/api/python/v2.3/semantics/reasoners/prescriptive/problem/problem_class/index.md#method-maximize)
         2. [minimize](/api/python/v2.3/semantics/reasoners/prescriptive/problem/problem_class/index.md#method-minimize)
   4) [std](/api/python/v2.3/semantics/std/index.md)1) [constraints](/api/python/v2.3/semantics/std/constraints/index.md)1. [unique](/api/python/v2.3/semantics/std/constraints/unique/index.md)
      2) [datetime](/api/python/v2.3/semantics/std/datetime/index.md)1) [date](/api/python/v2.3/semantics/std/datetime/date_class/index.md)1. [add](/api/python/v2.3/semantics/std/datetime/date_class/index.md#method-add)
            2. [subtract](/api/python/v2.3/semantics/std/datetime/date_class/index.md#method-subtract)
         2) [datetime](/api/python/v2.3/semantics/std/datetime/datetime_class/index.md)1) [add](/api/python/v2.3/semantics/std/datetime/datetime_class/index.md#method-add)
            2) [subtract](/api/python/v2.3/semantics/std/datetime/datetime_class/index.md#method-subtract)
      3) [DateTimeValue](/api/python/v2.3/semantics/std/datetimevalue/index.md)
      4) [DateValue](/api/python/v2.3/semantics/std/datevalue/index.md)
      5) [FloatValue](/api/python/v2.3/semantics/std/floatvalue/index.md)
      6) [IntegerValue](/api/python/v2.3/semantics/std/integervalue/index.md)
      7) [NumberValue](/api/python/v2.3/semantics/std/numbervalue/index.md)
      8) [StringValue](/api/python/v2.3/semantics/std/stringvalue/index.md)

## Returned By

1. [semantics](/api/python/v2.3/semantics/index.md) / [std](/api/python/v2.3/semantics/std/index.md)1) [datetime](/api/python/v2.3/semantics/std/datetime/index.md)1. [date](/api/python/v2.3/semantics/std/datetime/date_class/index.md)1) [range](/api/python/v2.3/semantics/std/datetime/date_class/index.md#method-range)
      2. [datetime](/api/python/v2.3/semantics/std/datetime/datetime_class/index.md)1) [range](/api/python/v2.3/semantics/std/datetime/datetime_class/index.md#method-range)
   2) [decimals](/api/python/v2.3/semantics/std/decimals/index.md)1) [decimal](/api/python/v2.3/semantics/std/decimals/decimal/index.md)
   3) [integers](/api/python/v2.3/semantics/std/integers/index.md)1) [int128](/api/python/v2.3/semantics/std/integers/int128/index.md)
      2) [int64](/api/python/v2.3/semantics/std/integers/int64/index.md)
   4) [numbers](/api/python/v2.3/semantics/std/numbers/index.md)1) [integer](/api/python/v2.3/semantics/std/numbers/integer/index.md)
      2) [number](/api/python/v2.3/semantics/std/numbers/number/index.md)
   5) [re](/api/python/v2.3/semantics/std/re/index.md)1) [RegexMatch](/api/python/v2.3/semantics/std/re/regexmatch_class/index.md)1. [end](/api/python/v2.3/semantics/std/re/regexmatch_class/index.md#method-end)
         2. [group](/api/python/v2.3/semantics/std/re/regexmatch_class/index.md#method-group)
         3. [group\_by\_name](/api/python/v2.3/semantics/std/re/regexmatch_class/index.md#method-group_by_name)
         4. [span](/api/python/v2.3/semantics/std/re/regexmatch_class/index.md#method-span)
         5. [start](/api/python/v2.3/semantics/std/re/regexmatch_class/index.md#method-start)
   6) [strings](/api/python/v2.3/semantics/std/strings/index.md)1) [split](/api/python/v2.3/semantics/std/strings/split/index.md)

## Referenced By

1. [RelationalAI Documentation](/index.md)1) [Build With RelationalAI](/build/index.md)1. [Build a semantic model](/build/modeling/index.md)1) [Derive facts with logic](/build/modeling/derive-facts-with-logic/index.md)1. [Write conditional definitions with `Model.where` and `Model.define`](/build/modeling/derive-facts-with-logic/index.md#write-conditional-definitions-with-modelwhere-and-modeldefine)
      2. [Use advanced reasoning](/build/reasoning/index.md) / [Rules-based reasoning](/build/reasoning/rules-based/index.md)1) [Work with numbers](/build/reasoning/rules-based/work-with-numbers/index.md)
   2) [Interact with RelationalAI](/interact/index.md)1) [Query a model with PyRel](/interact/query-a-model-with-pyrel/index.md)1. [Filter results with `where`](/interact/query-a-model-with-pyrel/index.md#filter-results-with-where)
