anonymous inline functions in python
Project description
fonc
multiline inline anonymous functions in python
>>> from fonc import fonc
>>> [
... fonc("""
... def x(input): # doesn't matter what this function is called
... value = input * 3.14
... return int(value)
... """)(value) for value in [1, 2, 3, 4]
... ]
[3, 6, 9, 12]
why would you do this terrible thing
:) :) :) :) :) :) :) :) :) :) :) :)
does this pollute my global namespace?
nope!
imagine this inline function:
>>> from fonc import fonc
>>> [
... fonc("""
... def x(input): # doesn't matter what this function is called
... value = input * 3.14
... return int(value)
... """)(value) for value in [1, 2, 3, 4]
... ]
[3, 6, 9, 12]
One might imagine that this means that the x
function name is overwritten to the global namespace. It is not!
>>> x(1)
NameError: name 'x' is not defined
how this works pls?
- parse the string and determine the name of the defined function
- replace that with a known random value
- replace calls to that inline function with calls to the renamed function
never-asked questions
-
Do I have access to global vars inside this function?
ya
-
Can I use this in production?
absolutely not u fool
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
fonc-0.2.0.tar.gz
(3.2 kB
view details)
File details
Details for the file fonc-0.2.0.tar.gz
.
File metadata
- Download URL: fonc-0.2.0.tar.gz
- Upload date:
- Size: 3.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | adb8db15011583e11deeafaa0903978011d16b0f77f5eef3c1fbc4182aac16c7 |
|
MD5 | 69ea6e5f495ec921cad114a334fce328 |
|
BLAKE2b-256 | e084f66194c6dc810052193a76360e087625f83dde1712ca966337f7ee471fd8 |