Bringing the fun of immediately-invoked function expressions to Python!
Reason this release was yanked:
Bug with typing. Please use equivalent 1.1.0
Project description
iife
Immediately-invoked function expressions in Python
The iife
package provides a decorator function iife
that calls the function/class it decorates and assigns the result to the name of the function/class.
The entire package:
This is the entire package (iife.py
):
from typing import Callable, TypeVar
T = TypeVar("T")
def iife(c: Callable[[], T]) -> T:
return c()
That's it.
Some use cases include...
Creating an anonymous object.
Have you ever written a class that you know will only have one instance? You can use the iife decorator to create that instance immediately.
from iife import iife
@iife
@dataclass
class player:
x: int = 1
y: int = 2
# player is an instance of the player class
player.x # -> 1
# The class cannot be reinstantiated because the name is shadowed.
new_player = player(x=3, y=4) # -> SyntaxError
This might also be useful in library development to hide the implementation details of the class from the end user, who can only access the single instance.
Complex initialization.
Sometimes a variable needs to be initialized by complex logic that cannot be expressed as a single assignment. Traditionally, this can be done with temporarily setting the variable to a default value:
x = None
y = [1, 2, 3]
for i in y:
if i == 2:
x = i
Why not do it with an IIFE? (To be honest, this isn't the best example, but it's more fun to do it like this.)
from iife import iife
@iife
def x() -> Optional[int]:
y = [1, 2, 3]
for i in y:
if i == 2:
return i
... And a bunch more. Maybe. Tbh this is mostly for fun.
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
Built Distribution
Hashes for invoke_iife-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 00889c46fae9c580d99da457694000242f7a923319565bbc26892435af33dfbc |
|
MD5 | efce69b0ff02701e2aac037e5cc018d9 |
|
BLAKE2b-256 | b6dec0363efdaf6091b82d1a855d53ab3ed819b4c64f2548241fcb2032457587 |