Skip to main content

Implicit parameters in Python

Project description

# Implicits
Global state can be hard to reason about, but piping dependencies from function to function is a pain. With implicits, you _explicitly_ which function parameters are _implicit_ dependencies. When you call the function, no need to explicilty provide these parameters; instead, the parameters will be implicitly passed! All that's required is that there exists local variables in scope that match the names of the parameters you're calling.
def create_task(title, *, current_user):
print(f"{current_user} created a task titled '{title}'")

current_user = "Jaden"
create_task("Hooray, a task!") # Jaden created a task titled 'Hooray, a task!'
create_task("Buy some trackpants") # Jaden created a task titled 'Buy some trackpants'

## Usage
1) Install via `pip install implicits`.
2) Import with `from implicits import implicits`.
3) Decorate using `@implicits("names", "of", "implicit", "parameters")`.

## Example
import logging
import boto3

from implicits import implicits

class Giraffe:
def __init__(self, name, *, logger): = name"Creating a Giraffe named {name}")

def full_name(self, *, logger):"Getting {}'s full name")
return f"{} the Giraffe"

def is_hungry(self, *, food):
return "leaves" in food

def main(*, logger):
jeff = Giraffe("Jeff") # Creating a Giraffe named Jeff
name = jeff.full_name # Getting Jeff's full name
food = ["rocks", "dirt"] # False
food.append("leaves") # True

logger = logging.getLogger()


## References

I didn't invent this idea! Quite a few other languages support implicit parameters. The most mainstream of these languages is Scala. [Check out how implicits work in Scala!](

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

implicits-1.0.2.tar.gz (2.5 kB view hashes)

Uploaded source

Built Distributions

implicits-1.0.2-py3-none-any.whl (3.7 kB view hashes)

Uploaded py3

implicits-1.0.2-py2-none-any.whl (2.8 kB view hashes)

Uploaded py2

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