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 dataclasses import dataclass
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.2.tar.gz (7.0 kB view hashes)

Uploaded Source

Built Distribution

unpackable-0.0.2-py2.py3-none-any.whl (7.8 kB view hashes)

Uploaded Python 2 Python 3

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