Skip to content

synt.stmt.namespace ¤

global_ module-attribute ¤

global_ = Global

Alias Global.

nonlocal_ module-attribute ¤

nonlocal_ = Nonlocal

Alias Nonlocal.

Global ¤

Bases: Statement

The global statement.

Examples:

global_stmt = global_(id_('foo'))
assert global_stmt.into_code() == 'global foo'
References

Global.

Source code in synt/stmt/namespace.py
class Global(Statement):
    """The `global` statement.

    Examples:
        ```python
        global_stmt = global_(id_('foo'))
        assert global_stmt.into_code() == 'global foo'
        ```

    References:
        [`Global`](https://docs.python.org/3/library/ast.html#ast.Global).
    """

    names: list[Identifier]
    """Global variable names."""

    def __init__(self, *names: Identifier):
        """Initialize a new `global` statement.

        Args:
            names: Global variable names.

        Raises:
            ValueError: If the `names` list is empty.
        """
        if len(names) == 0:
            raise ValueError("At least one global variable name is required.")
        self.names = list(names)

    def indented(self, indent_width: int, indent_atom: str) -> str:
        return f"{indent_atom * indent_width}global {', '.join(name.into_code() for name in self.names)}"

names instance-attribute ¤

Global variable names.

__init__ ¤

__init__(*names: Identifier)

Initialize a new global statement.

Parameters:

Name Type Description Default
names Identifier

Global variable names.

()

Raises:

Type Description
ValueError

If the names list is empty.

Source code in synt/stmt/namespace.py
def __init__(self, *names: Identifier):
    """Initialize a new `global` statement.

    Args:
        names: Global variable names.

    Raises:
        ValueError: If the `names` list is empty.
    """
    if len(names) == 0:
        raise ValueError("At least one global variable name is required.")
    self.names = list(names)

indented ¤

indented(indent_width: int, indent_atom: str) -> str
Source code in synt/stmt/namespace.py
def indented(self, indent_width: int, indent_atom: str) -> str:
    return f"{indent_atom * indent_width}global {', '.join(name.into_code() for name in self.names)}"

Nonlocal ¤

Bases: Statement

The nonlocal statement.

Examples:

nonlocal_stmt = nonlocal_(id_('foo'))
assert nonlocal_stmt.into_code() == 'nonlocal foo'
References

Nonlocal.

Source code in synt/stmt/namespace.py
class Nonlocal(Statement):
    """The `nonlocal` statement.

    Examples:
        ```python
        nonlocal_stmt = nonlocal_(id_('foo'))
        assert nonlocal_stmt.into_code() == 'nonlocal foo'
        ```

    References:
        [`Nonlocal`](https://docs.python.org/3/library/ast.html#ast.Nonlocal).
    """

    names: list[Identifier]
    """Nonlocal variable names."""

    def __init__(self, *names: Identifier):
        """Initialize a new `nonlocal` statement.

        Args:
            names: Nonlocal variable names.

        Raises:
            ValueError: If the `names` list is empty.
        """
        if len(names) == 0:
            raise ValueError("At least one nonlocal variable name is required.")
        self.names = list(names)

    def indented(self, indent_width: int, indent_atom: str) -> str:
        return f"{indent_atom * indent_width}nonlocal {', '.join(name.into_code() for name in self.names)}"

names instance-attribute ¤

Nonlocal variable names.

__init__ ¤

__init__(*names: Identifier)

Initialize a new nonlocal statement.

Parameters:

Name Type Description Default
names Identifier

Nonlocal variable names.

()

Raises:

Type Description
ValueError

If the names list is empty.

Source code in synt/stmt/namespace.py
def __init__(self, *names: Identifier):
    """Initialize a new `nonlocal` statement.

    Args:
        names: Nonlocal variable names.

    Raises:
        ValueError: If the `names` list is empty.
    """
    if len(names) == 0:
        raise ValueError("At least one nonlocal variable name is required.")
    self.names = list(names)

indented ¤

indented(indent_width: int, indent_atom: str) -> str
Source code in synt/stmt/namespace.py
def indented(self, indent_width: int, indent_atom: str) -> str:
    return f"{indent_atom * indent_width}nonlocal {', '.join(name.into_code() for name in self.names)}"