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 hashes)

Uploaded source

Built Distribution

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

Uploaded py2 py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page