Skip to main content

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.

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.

Release files for invoke-iife 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for invoke-iife 1.1.0
File Size Uploaded
invoke-iife-1.1.0.tar.gz 3.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for invoke-iife 1.1.0
File Interpreter ABI Platform
invoke_iife-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 7.7 kB

Release files / invoke-iife-1.1.0.tar.gz

Download URL invoke-iife-1.1.0.tar.gz
Size 3.7 kB
Tags Source
SHA-256 checksum
How to use checksums
8568b4a9dd0cb1f79d5fd3b41bca05711c8cee1247f692d00620103a07ddf65d
BLAKE2b-256 checksum
How to use checksums
1c0baa2902965ade1c93a1ff76f9df38e53964779e3e054d11928a14ed2033f8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.14 CPython/3.11.0b3 Windows/10

Release files / invoke_iife-1.1.0-py3-none-any.whl

Download URL invoke_iife-1.1.0-py3-none-any.whl
Size 4.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6353585ef1dbacb9345953401d275e9ecd9445307649c3b2a157451146194e44
BLAKE2b-256 checksum
How to use checksums
79216cee8cd8b7576e5cbd21cffb6152f43d149c2996262f025ec8e1aef03a4d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.14 CPython/3.11.0b3 Windows/10

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page