Skip to main content

Keyword-only argument support for Python 2 as a decorator. Python 3 compatible.

Project description

license: MIT

This library emulates the python3 keyword-only arguments under python2. The resulting code is python3 compatible.

Usage

Installation

pip install kwonly-args

Alternatively you can download the zipped library from https://pypi.python.org/pypi/kwonly-args

Code

With this library you can turn some or all of the default arguments of your function into keyword-only arguments.

  • Decorate your function with kwonly_args.first_kwonly_arg and select one of the default arguments of your function with the name parameter of the decorator. The selected argument along with all default arguments on its right side will be treated as keyword-only arguments.

  • All keyword-only arguments have a default value and they aren’t required args by default. You can make a keyword-only argument required by using kwonly_args.KWONLY_REQUIRED as its default value.

Your new-born keyword-only args are no longer treated as positional arguments and varargs still work if your function has *args or something like that.

from kwonly_args import first_kwonly_arg, KWONLY_REQUIRED


# This turns default1 and default2 into keyword-only arguments.
# They are no longer handled as positional arguments.
@first_kwonly_arg('default1')
def func(arg0, arg1, default0='d0', default1='d1', default2='d2', *args):
    print('arg0={} arg1={} default0={} default1={} default2={} args={}'.format(
          arg0, arg1, default0, default1, default2, args))


func(0, 1, 2, 3, 4)
# Output:
# arg0=0 arg1=1 default0=2 default1=d1 default=d2 args=(3, 4)

# The default1 and default2 args can be passed only as keyword arguments:
func(0, 1, 2, 3, 4, default1='kwonly_param')
# Output:
# arg0=0 arg1=1 default0=2 default1=kwonly_param default=d2 args=(3, 4)


# In this example all three args are keyword-only args and default1 is required.
@first_kwonly_arg('default0')
def func2(default0='d0', default1=KWONLY_REQUIRED, default2='d2'):
    ...

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

kwonly-args-1.0.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kwonly_args-1.0.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file kwonly-args-1.0.0.tar.gz.

File metadata

  • Download URL: kwonly-args-1.0.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for kwonly-args-1.0.0.tar.gz
Algorithm Hash digest
SHA256 02cff4850d35aa7e99f836c9ab272a300cde160e311208fff7dfd049caaa548a
MD5 237dca6b405255b2a345af25b2c8cff4
BLAKE2b-256 f1fbacfb0ce54a13494400e246e65c073cbbee64f5fe54446528f3f6e32287d7

See more details on using hashes here.

File details

Details for the file kwonly_args-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for kwonly_args-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f192d9092ef0f9f0c290e9a0336d362e52bdaae4aafad7e0260572855e4253a8
MD5 1856109101477e1ae09c37c85ca3327e
BLAKE2b-256 87d6a8880ddca565002a7381680f4bf7aa4fdc2972c65590c9b0584d99cabdaf

See more details on using hashes here.

Supported by

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