Skip to main content

The bindpy package allows for partial application of arguments to a function, making it easy to create specialized versions of the function with some arguments pre-filled.

Project description

bindpy

The bindpy package allows for partial application of arguments to a function, making it easy to create specialized versions of the function with some arguments pre-filled. It is a better version of the Python's standard partial function from the functools package inspired by C++'s std::bind.

Install

Expect gracefully crafted support for any version of Python 3+, but confidently tested in version 3.7 and higher.

pip install bindpy

Usage

from bindpy import *

bind function

The show function takes three arguments, a1, a2 and a3, and returns a string composed of their values separated by spaces. The show_10 function is a partially applied version of show, with a2 bound to _1, a1 bound to _2 and a3 bound to 10.

Bind support placeholders : _1, _2, ... _10. The placeholders allow you to partially apply a function and leave certain arguments to be filled in later. This allows you to reuse the partially applied function and pass different values for the placeholder argument.

Overall, bind and placeholders make it easier to create reusable and composable functions by allowing you to fix certain arguments and create new functions that take fewer arguments.

def show(a1, a2, a3):
    return " ".join(map(str, [a1, a2, a3]))
    
show_10 = bind(show, _2, _1, a3=10)

print(show_10(20, 30)) # output: 30 20 10

Convenient to use with functional style.

If you find lambda expressions unappealing, you can use bind for a more convenient and aesthetically pleasing experience with functional programming.

def add(a, b, c):
  return a + b * c
  
numbers = [1, 2, 3, 4]
print(list(map(bind(add, _1, 10, 2), numbers))) # output 21 22 23 24
# same code with lambda
print(list(map(lambda x: add(x, 10, 2), numbers)))
import os # used for example

files = ['a.txt', 'b.json']
my_join = bind(os.path.join, '.', 'data')
print(list(map(my_join, files))) # output ['./data/a.txt', './data/b.json']

bind_front function

bind_front pre-specifies function arguments like functools.partial. It takes a function and values, returns new function with values bound to front. When called with remaining args, values passed to bind_front are automatically inserted in front.

def add(a, b, c=0):
    return a + b + c


add_10 = bind_front(add, 10)
result = add_10(20, c=30)
print(result)  # 60

add_20_30 = bind_front(add, 20, 30)
result = add_20_30() # call add(20, 30)
print(result)  # 50

bind_back function

bind_back also pre-specifies function arguments but with values bound to end of arg list after all others. It takes a function and key-value pairs, returns new function with values bound to end. When called with remaining args, values passed to bind_back are automatically inserted at end.

add_30 = bind_back(add, c=30)
result = add_30(10, 20)
print(result)  # 60

add_40 = bind_back(add, 40)
result = add_40(10, 20) # call add(10, 20, 40)
print(result)  # 70

add_10 = bind_back(add, 10)
result = add_10(12)  # call add(10, 12), c=0 by  default
print(result)  # 22

sequential binding

You can combine bind_front and bind_back to create a function that has arguments pre-specified at both the front and end of the argument list. For example, the code:

def func(p1, p2, p3, p4, p5):
    return " ".join(map(str, [p1, p2, p3, p4, p5]))
    
b_func = bind_front(bind_back(func, 4, 5), 1, 2)
print(bfunc(3)) # 1 2 3 4 5

can be replaced with:

b_func_v2 = bind(1, 2, _1, 4, 5) # using placeholder *_1*
print(bfunc(3)) # 1 2 3 4 5

We hope this information helps you effectively use the bind functions in your project. If you have any questions or feedback, please reach out to us. Happy coding!


Acknowledgements

We would like to express our sincere gratitude to all the individuals who have made this project a reality. Their contributions, guidance, and support have been invaluable. Thank you to everyone who has played a part in bringing this project to life.

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

bindpy-0.1.1.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

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

bindpy-0.1.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file bindpy-0.1.1.tar.gz.

File metadata

  • Download URL: bindpy-0.1.1.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.10.5 Windows/10

File hashes

Hashes for bindpy-0.1.1.tar.gz
Algorithm Hash digest
SHA256 0015f018f9e319192b4cd5a6da378662058798940dfd7bd31963e366a2ee4dcf
MD5 b20539a5110f13df36f8d82c59ec9faa
BLAKE2b-256 e36a7a85098402c39e4d9d490bdd766c8e07811fde620ecd50ca4abe0c3cc0a3

See more details on using hashes here.

File details

Details for the file bindpy-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: bindpy-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.10.5 Windows/10

File hashes

Hashes for bindpy-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bc40f7de5adc773f2de1bc59879cc7933575d38ddf60669ab113df8346fbd668
MD5 7b1deab42b0c8a774c273fb381e5ea30
BLAKE2b-256 38369e69c060474bcabdea3fd8c45cdcdfb632d369f5b8ec1f0297bb0d721ecf

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