Convert any callable into a function.
Project description
Overview
Convert any callable into a function. Useful to circumvent method binding.
Installation
To install tofunc, you can use pip. Open your terminal and run:
pip install tofunc
Implementation
import functools
__all__ = ["tofunc"]
def tofunc(old, /):
def new(*args, **kwargs):
return old(*args, **kwargs)
try:
new = functools.wraps(old)(new)
except:
pass
return new
Example
from tofunc import tofunc
import functools
def join(self, b="beta", c="gamma"):
return "%s %s %s" % (self, b, c)
hello = functools.partial(join, c="hello")
class Foo:
...
Foo.greet = hello
print(Foo().greet("Alice"))
# Output: Alice beta hello
hello = tofunc(hello)
Foo.greet = hello
print(Foo().greet("Bob"))
# Output: <__main__.Foo object at 0x1443bfce0> Bob hello
License
This project is licensed under the MIT License.
Links
Credits
Author: Johannes
Thank you for using tofunc!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
tofunc-1.0.0.tar.gz
(3.1 kB
view details)
Built Distribution
File details
Details for the file tofunc-1.0.0.tar.gz
.
File metadata
- Download URL: tofunc-1.0.0.tar.gz
- Upload date:
- Size: 3.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 957cc8fa86ed57f8d3bca6cc8b8bd9e023c5c072d827024648f956db0a316c46 |
|
MD5 | 0cbe218f66de8e222f7d878e2aa7f69c |
|
BLAKE2b-256 | 89615cc1e0a8618db039da4c1ea00e72857d1393c73e022730e037f43bbe5f1c |
File details
Details for the file tofunc-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: tofunc-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 294a205522abcf6a7cde7d9b2f6d67b02a17b462457687f5d08306bf77478057 |
|
MD5 | 159d76d5bd34bf12e8ead0fe156fe068 |
|
BLAKE2b-256 | 0d1f6df616575d86bfca6ab8134f91cf41cf2db910cd2d4a8b580492f4a2bc70 |