Skip to main content

@export decorator that adds a function or class to __all__

Project description

Explicit module exports

@export - The DRY alternative to __all__


PyPI version shields.io PyPI pyversions PyPI license


Installation

pip install python-exports

Usage

>>> from exports import export

Now you can use it to add to __all__ as

  • function decorator

    >>> @export
    ... def spam():
    ...     ...
    
  • class decorator:

    >>> @export
    ... class Ham:
    ...     ...
    
  • by name:

    >>> from functools import reduce as fold
    >>> export('fold')
    

Behaviour

If the module has no all, it is created. Otherwise, __all__ is converted to a list, and the export is appended.

Caveats

Exporting a function or class directly relies on the name attribute, so consider the following example:

>>> def eggs():
...     ...
>>> fake_eggs = eggs

If we want to export fake_eggs, then this will not work:

>>> export(fake_eggs)  # BAD: this will add 'eggs' to __all__

In such cases, use the name instead:

>>> export('fake_eggs')  # GOOD

You'll be safe if you either

  • decorate a function or a class directly with @export,
  • pass the name string when using plain export('...') calls.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_exports-1.1.1.tar.gz (3.2 kB view hashes)

Uploaded Source

Built Distribution

python_exports-1.1.1-py3-none-any.whl (3.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page