Skip to main content

Apply values to optional params

Project description


Apply default values to functions.

Makes configuration easy! Application settings come from a config file into your code cleanly.

pip install apply_defaults


This decorator applies options from a ConfigParser object.

from apply_defaults import apply_config
from configparser import ConfigParser

config = ConfigParser()
config.read_dict({"general": {"option": True}})

def func(option: bool = False) -> bool:
    return option

The option parameter takes the value from the configuration.

>>> func()

Override the configuration by passing a value.

>>> func(option=False)

If the option is not in the configuration, the default value from the parameter list is used.

>>> config.remove_option("general", "option")
>>> func()

ConfigParser's options are strings. Type hints in the function signature allow the apply_config decorator to cast options to the desired type. Alternatively cast the value yourself.


This decorator applies attributes from the bound object.

from apply_defaults import apply_self

class MyObject:
    def __init__(self):
        self.option = True

    def func(self, option=False):
        return value

The parameter takes the value from the bound object, i.e.

>>> obj = MyObject()
>>> obj.func()

Override by passing a value.

>>> obj.func(option=False)

If the attribute is not in the bound object, the default value from the parameter list is used.

>>> del obj.option
>>> obj.func()

Project details

Download files

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

Files for apply-defaults, version 0.1.4
Filename, size File type Python version Upload date Hashes
Filename, size apply_defaults-0.1.4.tar.gz (3.1 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page