Skip to main content

📦 Destructure Python objects

Project description

Unpackable: Object destructuring for Python

unpackable is a module that lets you use Python's destructuring assignment to unpack an object's attributes.

Use case

Consider JavaScript's destructuring assignment feature that allows you to do the following:

class User {
  constructor(id, email) {
    this.id = id;
    this.email = email;
  }
}

const user = new User(1, 'example@example.com')
const {id, email} = user;  // destructure

unpackable allows you to do something similar in Python:

from dataclasses import dataclass
from unpackable import Unpackable


@dataclass
class User(Unpackable):
  id: int
  email: str


user = User(1, 'example@example.com')
id, email = user  # destructure

unpackable can also unpack objects that don't subclass Unpackable:

from unpackable import unpack


@dataclass
class User:
  id: int
  email: str


user = User(1, 'example@example.com')
id, email = unpack(user)  # destructure

Status

unpackable currently works with iterable objects, dataclasses and simple objects.

This is alpha software and is not ready for use beyond limited use cases like in my app_paths project.

Installation

Requirements

  • Python 3.8+

PyPI

python3 -m pip install unpackable

Support

Want to support this project and other open-source projects like it?

Buy Me A Coffee

License

See LICENSE. If you'd like to use this project with a different license, please get in touch.

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

unpackable-0.0.4.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

unpackable-0.0.4-py2.py3-none-any.whl (7.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file unpackable-0.0.4.tar.gz.

File metadata

  • Download URL: unpackable-0.0.4.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for unpackable-0.0.4.tar.gz
Algorithm Hash digest
SHA256 55185896c71f607f43e8cbe22913e46083baf2371ecd1c8a06f0bf93ab76f38d
MD5 71107dd60ca83b2a9dad63fc6d522273
BLAKE2b-256 b01da7b462d4ef897e1119acc86e6426b39b7c59eea79e3d08a0cf0b43fbbda2

See more details on using hashes here.

File details

Details for the file unpackable-0.0.4-py2.py3-none-any.whl.

File metadata

  • Download URL: unpackable-0.0.4-py2.py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for unpackable-0.0.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 addb1bc7d01d8d1b64a0fc41a5f05bb49a62fe8fe730630d2cd04e4231cfc661
MD5 785b0407b7d80d6c1bdefd0cc7879af7
BLAKE2b-256 18a7549e3e8d13181c137a9174a128c33dcbfc77efd5f457b82054b96f768143

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page