rberga06.utils.access.access#

class rberga06.utils.access.access(ns: dict[str, Any] | None = None, /)#

Bases: _access[_X]

Access specifiers.

Parameters:

ns – The namespace in which to operate. Defaults to the caller’s locals().

Example:

>>> locals().clear()
>>> from rberga06.utils.access import *
>>> __all__
Traceback (most recent call last):
...
NameError: name '__all__' is not defined
>>> a = access()
>>> @a.public
... def _foo(x: int) -> int:
...     return x
...
>>> @a.private
... def priv(y: str) -> str:
...     return y
...
>>> with a.public:
...     _x: str = "public"
...
>>> with a.private:
...     y: int = 42
...
>>> __all__  # it's now magically defined
['_foo', '_x']
>>> __all__ is a.all
True

Important

When using the context manager syntax (for example, with a.public: blocks), the access instance only knows about new assignments. This means you really should not re-assign variables in a with a.public: or with a.private: block. For example:

>>> # This is ok
>>> with a.public:
...     x: int = 0
...
>>> x = 42
>>>
>>> # This is not
>>> x: int = 0
>>> with a.public:
...     x: int = 42
>>>
__init__(ns: dict[str, Any] | None = None, /) None#

Methods

__init__([ns])

Attributes

all

The attached __all__.

ns

The attached namespace.

private

Mark something private

public

Mark something public

property all: list[str]#

The attached __all__.

property ns: dict[str, _X]#

The attached namespace.

property private: private[_X]#

Mark something private

property public: public[_X]#

Mark something public