Skip to main content

Bringing the fun of immediately-invoked function expressions to Python!

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.

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

invoke-iife-1.1.0.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

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

invoke_iife-1.1.0-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file invoke-iife-1.1.0.tar.gz.

File metadata

  • Download URL: invoke-iife-1.1.0.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.11.0b3 Windows/10

File hashes

Hashes for invoke-iife-1.1.0.tar.gz
Algorithm Hash digest
SHA256 8568b4a9dd0cb1f79d5fd3b41bca05711c8cee1247f692d00620103a07ddf65d
MD5 80c52f16a1068100eedfcf6e51beec1e
BLAKE2b-256 1c0baa2902965ade1c93a1ff76f9df38e53964779e3e054d11928a14ed2033f8

See more details on using hashes here.

File details

Details for the file invoke_iife-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: invoke_iife-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.11.0b3 Windows/10

File hashes

Hashes for invoke_iife-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6353585ef1dbacb9345953401d275e9ecd9445307649c3b2a157451146194e44
MD5 5007becb31683eb187d5bd90fdfaab7c
BLAKE2b-256 79216cee8cd8b7576e5cbd21cffb6152f43d149c2996262f025ec8e1aef03a4d

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