A declaration scope of one module (Module.scope).
Functions#
__init__#
__init__(owner=None, parent=None) -> None| Parameter | Type | Description |
|---|---|---|
owner | object | None = None | |
parent | Scope | None = None |
def __init__(self, owner: object | None = None, parent: Scope | None = None) -> None:
self._owner = object() if owner is None else owner
self._parent = parent
self._taken: set[str] = set()
self._nested: set[str] = set()nested#
nested() -> ScopeReturns Scope
def nested(self) -> Scope:
return Scope(self._owner, self)in_module_of#
in_module_of(other) -> bool| Parameter | Type | Description |
|---|---|---|
other | Scope |
Returns bool
def in_module_of(self, other: Scope) -> bool:
return self._owner is other._ownerreserve#
reserve(name) -> None| Parameter | Type | Description |
|---|---|---|
name | str |
def reserve(self, name: str) -> None:
self._taken.add(name)
current = self._parent
while current is not None:
current._nested.add(name)
current = current._parenttaken_by#
taken_by(name) -> str | NoneWhy name cannot be declared here, or None when it can.
| Parameter | Type | Description |
|---|---|---|
name | str |
Returns str | None
def taken_by(self, name: str) -> str | None:
"""Why ``name`` cannot be declared here, or ``None`` when it can."""
if name in self._nested:
return "a nested scope declares it"
current: Scope | None = self
while current is not None:
if name in current._taken:
return (
"this scope declares it"
if current is self
else "an enclosing scope declares it"
)
current = current._parent
return Nonenames#
names() -> set[str]Returns set[str]
def names(self) -> set[str]:
return self._taken | self._nested