79

In Javascript, I can use destructuring to extract properties I want from a javascript objects in one liner. For example:

currentUser = {
  "id": 24,
  "name": "John Doe",
  "website": "http://mywebsite.com",
  "description": "I am an actor",
  "email": "[email protected]",
  "gender": "M",
  "phone_number": "+12345678",
  "username": "johndoe",
  "birth_date": "1991-02-23",
  "followers": 46263,
  "following": 345,
  "like": 204,
  "comments": 9
}

let { id, username } = this.currentUser;
console.log(id) // 24
console.log(username) //johndoe

Do we have something similar in Python for Python dicts and Python objects? Example of Python way of doing for python objects:

class User:
    def __init__(self, id, name, website, description, email, gender, phone_number, username):
        self.id = id
        self.name = name
        self.website = website
        self.description = description
        self.email = email
        self.gender = gender
        self.phone_number = phone_number
        self.username = username
  
current_user = User(24, "Jon Doe", "http://mywebsite.com", "I am an actor", "[email protected]", "M", "+12345678", "johndoe")
    
# This is a pain
id = current_user.id
email = current_user.email
gender = current_user.gender
username = current_user.username
    
print(id, email, gender, username)

Writing those 4 lines (as mentioned in example above) vs writing a single line (as mentioned below) to fetch values I need from an object is a real pain point.

(id, email, gender, username) = current_user
10
  • print(currentUser['id']) in Python?
    – DirtyBit
    Commented Feb 20, 2019 at 11:24
  • #e.dan this question is more broad - covers dict and objects both.
    – ThinkGeek
    Commented Feb 20, 2019 at 11:33
  • 1
    Regarding your 2nd question (User), see the attrs library: attrs.org/en/stable. Or there is this approach: self.__dict__.update(locals())
    – FMc
    Commented Feb 20, 2019 at 11:33
  • If you're printing those attributes in a certain way every time for various instances of User, you could define a custom __str__. If you just need those attributes in a group from time to time, you could define a method that returns such. Commented Feb 20, 2019 at 11:37
  • 3
    Why do you need those "painful" lines in the first place? Can't you just access the values from the object?
    – AKX
    Commented Feb 20, 2019 at 11:37

8 Answers 8

72

You can use operator module from standard library as follows:

from operator import attrgetter
id, email, gender, username = attrgetter('id', 'email', 'gender', 'username')(current_user)
print(id, email, gender, username)

In case you have a dict like from your example

currentUser = {
  "id": 24,
  "name": "John Doe",
  "website": "http://mywebsite.com",
  "description": "I am an actor",
  "email": "[email protected]",
  "gender": "M",
  "phone_number": "+12345678",
  "username": "johndoe",
  "birth_date": "1991-02-23",
  "followers": 46263,
  "following": 345,
  "like": 204,
  "comments": 9
}

just use itemgetter instead of attrgetter:

from operator import itemgetter
id, email, gender, username = itemgetter('id', 'email', 'gender', 'username')(currentUser)
print(id, email, gender, username)
6
  • 4
    This almost works. This remains dependant in the order in which the variables are set. i.e. email, id = itemgetter('id', 'email')(currentUser) Assigns incorrect values to email and id. In javascript as the example shows, order is irrelevant when setting the variables.
    – M.Nar
    Commented Aug 24, 2021 at 18:08
  • @M.Nar As you can see, all the answers here either destruct an object completely (you should not mess up with the order which could be unpredicted) or suppose that you provide fields you wanna get and in which order (not mess up with the order provided by yourself is much easier). Well, Python and JS are different languages, you can't argue with it. On the other hand, Python allows you to rename values received from the object while in JS the names will be the same as in the original object. Commented Aug 24, 2021 at 18:44
  • 4
    @LevRomanov Note, you can rename the values received from the object in JS. The syntax is let { originalName: newName } = obj;
    – mb7744
    Commented Jan 5, 2022 at 0:59
  • @LevRomanov is it possible to get values from nested dictionary using itemgetter as we do in JS Commented Jan 12, 2022 at 4:54
  • @AzharUddinSheikh it seems that you can not do it with dicts using itemgetter, but you actually can with classes and attrgetter — just use . as delimiter: id, email, gender, username = attrgetter('id', 'contact_info.email', 'gender', 'username')(current_user) Commented Feb 1, 2022 at 22:01
30

Building off of other answers, I would recommend also using Python's dataclasses and use __getitem__ to get specific fields:

from dataclasses import astuple, dataclass

@dataclass
class User:
    id: int
    name: str
    website: str
    description: str
    email: str
    gender: str
    phone_number: str
    username: str
    
    def __iter__(self):
        return iter(astuple(self))
    
    def __getitem__(self, keys):
        return iter(getattr(self, k) for k in keys)
        

current_user = User(id=24, name="Jon Doe", website="http://mywebsite.com", description="I am an actor", email="[email protected]", gender="M", phone_number="+12345678", username="johndoe")

# Access fields sequentially:
id, _, email, *_ = current_user
# Access fields out of order:
id, email, gender, username = current_user["id", "email", "gender", "username"]
4
  • Only available in Py3
    – ThinkGeek
    Commented Sep 25, 2020 at 8:20
  • 27
    Yes, well, it might be time to upgrade to Python 3: "As of January 1st, 2020 no new bug reports, fixes, or changes will be made to Python 2, and Python 2 is no longer supported." python.org/doc/sunset-python-2
    – Carl G
    Commented Mar 31, 2021 at 8:34
  • One downside of this approach I've discovered is that the astuple call will convert nested dataclasses into tuples as well.
    – Carl G
    Commented Oct 20, 2021 at 19:04
  • 3
    Not available in Python 1. Commented Feb 25, 2023 at 6:02
22

In Python 3.10 and upwards you can do it using match, which allows variable names to be created in the pattern definition after case, as seen here:

from dataclasses import dataclass

@dataclass
class User:
    id: int
    username: str
    height: float

current_user = User(23, 'hobycat', 24.6)

match current_user:
    case User(id=id, username=username):
        # From here, id = current_user.id, username = current_user.username
        print(f"Found user with id {id} called {username}")

print(f"Outside the block, {username} is still bound")

As is visible above, the variables remain bound after the case block, so this can be used as a form of assignment:

match current_user:
    case User(id=id, username=username): pass

print(f"User {username} has id {id}")

Note: it's also possible to use positional class matching: case User(id, username), but this will not check the attribute names, so case User(username, id) will match them unexpectedly.

And it's also possible to give the matched attributes different names: case User(id=found_id, username=found_username), in which case found_id and found_username will be the variables accessible from the case block.

See the Python docs on match statements and the precise definition of class patterns in the PEP

1
  • I've added further text to explain this more clearly and show how this can be used. It does achieve what it claims, but to my mind still presents slightly unnaturally in the Python idiom Commented Oct 23, 2023 at 11:26
9

Don't flatten the arguments in the first place. When you write a 8-ary function like you did with User, you're bound to make mistakes like passing arguments in the wrong order.

Which of the following will produce User you intend?

  1. User(24, "Jon Doe", "http://mywebsite.com", "I am an actor", "[email protected]", "M", "+12345678", "johndoe")
  2. User(24, "Jon Doe", "http://mywebsite.com", "I am an actor", "[email protected]", "+12345678", "M", "johndoe")

Impossible to know! If your function takes a descriptor, you do not have this problem -

class User:
  def __init__ (self, desc = {}):
    self.desc = desc # whitelist items, if necessary
  
  def __str__ (self):
    # invent our own "destructuring" syntax
    [ name, age, gender ] = \
      destructure(self.desc, 'name', 'age', 'gender')

    return f"{name} ({gender}) is {age} years old"

# create users with a "descriptor"
u = User({ 'age': 2, 'gender': 'M' })
v = User({ 'gender': 'F', 'age': 3 })
x = User({ 'gender': 'F', 'name': 'Alice', 'age': 4 })

print(u) # None (M) is 2 years old
print(v) # None (F) is 3 years old
print(x) # Alice (F) is 4 years old

We can define our own destructure as -

def destructure (d, *keys):
  return [ d[k] if k in d else None for k in keys ]

This still could result in long chains, but the order is dependent on the caller, therefore it's not fragile like the 8-ary function in the original question -

[ name, age, gender ] = \
  destructure(self.desc, 'name', 'age', 'gender')

# works the same as

[ gender, name, age ] = \
  destructure(self.desc, 'gender', 'name', 'age')

Another option is to use keyword arguments -

class User:
  def __init__ (self, **desc):
    self.desc = desc # whitelist items, if necessary

  def __str__ (self):
    [ name, age, gender ] = \
      destructure(self.desc, 'name', 'age', 'gender')

    return f"{name} ({gender}) is {age} years old"

# create users with keyword arguments
u = User(age = 2, gender = 'M')
v = User(gender = 'F', age = 3)
x = User(gender = 'F', name = 'Alice', age = 4)

print(u) # None (M) is 2 years old
print(v) # None (F) is 3 years old
print(x) # Alice (F) is 4 years old
5
  • Thanks for the answer. What is your say for a function with keyword args, for example User(name = "Loki", gender="M", age="23")? Is kwargs way preferred over User({ 'name':'Loki', 'age': 23, 'gender': 'M' })?
    – ThinkGeek
    Commented Feb 21, 2019 at 18:24
  • Thanks, the only downside is, since we are storing everything inside self.attr, it might not be intuitive for people to understand this class and this will need overriding property getters and setters too.
    – ThinkGeek
    Commented Feb 21, 2019 at 18:35
  • Using method parameters with mutable collections as default values is a bad habit. Use None instead. If you declare def __init__ (self, desc = {}): and modify some instance's user1.desc, like user1 = User(); user1.desc['id'] = 1, then all new instances with default constructor call will have this value: user2 = User(); print(user2.desc) => {'id': 1} Commented Mar 10, 2021 at 17:06
  • 1
    It's only a bad habit if you have other bad habits like mutating variables :D I use functional discipline so I don't have the same set of worries as others.
    – Mulan
    Commented Mar 10, 2021 at 18:11
  • You can enforce keyword-only arguments, so hiding it all in an extra dictionary brings zero benefit IMHO.
    – polwel
    Commented Apr 29, 2022 at 10:30
6

(Ab)using the import system

Python already has a compact destructuring syntax in the form of from x import y. This can be re-purposed to destructure dicts and objects:

import sys, types

class MyClass:
    def __init__(self, a, b):
        self.a = a
        self.b = b

sys.modules["myobj"] = MyClass(1, 2)
from myobj import a, b
assert a + b == 3

mydict = {"c": 3, "d": 4}
sys.modules["mydict"] = types.SimpleNamespace(**mydict)
from mydict import c, d
assert c + d == 7

Cluttering sys.modules with our objects isn't very nice though.

Context manager

A more serious hack would be a context manager that temporarily adds a module to sys.modules, and makes sure the __getattr__ method of the module points to the __getattribute__ or __getitem__ method of the object/dict in question.

That would let us do:

mydict = {"a": 1, "b": 2}

with obj_as_module(mydict, "mydict"):
    from mydict import a, b

assert a + b == 3
assert "mydict" not in sys.modules

Implementation:

import sys, types
from contextlib import contextmanager

@contextmanager
def obj_as_module(obj, name):
    "Temporarily load an object/dict as a module, to import its attributes/values"
    module = types.ModuleType(name)
    get = obj.__getitem__ if isinstance(obj, dict) else obj.__getattribute__
    module.__getattr__ = lambda attr: get(attr) if attr != "__path__" else None
    try:
        if name in sys.modules:
            raise Exception(f"Name '{name}' already in sys.modules")
        else:
            sys.modules[name] = module
            yield module
    finally:
        if sys.modules[name] == module:
            del sys.modules[name]

This was my first time playing around with the import system, and I have no idea if this might break something, or what the performance is like. But I think it is a valuable observation that the import statement already provides a very convenient destructuring syntax.

Replacing sys.modules entirely

Using an even more questionable hack, we can arrive at an even more compact syntax:

with from_(mydict): import a, b

Implementation:

import sys

@contextmanager
def from_(target):
    "Temporarily replace the sys.modules dict with target dict or it's __dict__."
    if not isinstance(target, dict):
        target = target.__dict__
    sysmodules = sys.modules
    try:
        sys.modules = target
        yield
    finally:
        sys.modules = sysmodules

Class decorator

For working with classes we could use a decorator:

def self_as_module(cls):
    "For those who like to write self-less methods"
    cls.as_module = lambda self: obj_as_module(self, "self")
    return cls

Then we can unpack attributes without cluttering our methods with lines like a = self.a:

@self_as_module
class MyClass:
    def __init__(self):
        self.a = 1
        self.b = 2

    def check(self):
        with self.as_module():
            from self import a, b
        assert a + b == 3

MyClass().check()

For classes with many attributes and math-heavy methods, this is quite nice.

5

In this way JavaScript has better domain of objects than Python. You also can build a method or function to replicate the functionality, but JavaScript do it really easy.

Something similar on Python could be "packing/unpacking" functionalities applied to dictionaries (JSON objects).

You can find related documentation on the internet: https://www.geeksforgeeks.org/packing-and-unpacking-arguments-in-python/

4

You can implement an __iter__ method to enable unpacking:

class User:
  def __init__(self, **data):
    self.__dict__ = data
  def __iter__(self):
    yield from [getattr(self, i) for i in ('id', 'email', 'gender', 'username')]

current_user = User(**currentUser)
id, email, gender, username = current_user
print([id, email, gender, username])

Output:

[24, '[email protected]', 'M', 'johndoe']

Edit: Python2 solution:

class User:
  def __init__(self, **data):
    self.__dict__ = data
  def __iter__(self):
    for i in ('id', 'email', 'gender', 'username'):
      yield getattr(self, i)

Edit 2:

Getting select attributes:

class User:
  def __init__(self, **data):
     self.__dict__ = data
  def __getattr__(self, _vals):
     yield from [getattr(self, i) for i in _vals.split('_')]

current_user = User(**currentUser)
id, email, gender, username = current_user.id_email_gender_username
id, gender = current_user.id_gender
9
  • This won't work, if I want to unpack only limited fields from the object. For example: id, email = current_user This throws "ValueError: too many values to unpack"
    – ThinkGeek
    Commented Feb 21, 2019 at 7:27
  • @LokeshAgrawal You can use a throwaway variable: id, email, *_ = current_user
    – Ajax1234
    Commented Feb 21, 2019 at 17:19
  • Oh ok thanks for the suggestion, I think "yield from" syntax is not correct?
    – ThinkGeek
    Commented Feb 21, 2019 at 17:24
  • @LokeshAgrawal No, yield from is correct, it is creating a generator object that can be iterated over later
    – Ajax1234
    Commented Feb 21, 2019 at 17:26
  • @LokeshAgrawal Yes, however, I added a solution for Python2
    – Ajax1234
    Commented Feb 21, 2019 at 17:35
1

You can destruct a python dictionary and extract properties by unpacking with .values() method:

 currentUser = {
  "id": 24,
  "name": "John Doe",
  "website": "http://mywebsite.com",
  "description": "I am an actor",
  "email": "[email protected]",
  "gender": "M",
  "phone_number": "+12345678",
  "username": "johndoe",
  "birth_date": "1991-02-23",
  "followers": 46263,
  "following": 345,
  "like": 204,
  "comments": 9
}

id, _, _, _, _, _, _, username, *other = currentUser.values()

print('distructuring:', { 'id': id, 'username': username })
3
  • 4
    Will this break if the dictionary was created dynamically and the calling code does not know the insertion order or the dictionary items? Commented Nov 6, 2020 at 14:51
  • Probably, break. You are correct, this works if the dictionary structure and order are static
    – Roman
    Commented Apr 24, 2021 at 18:11
  • Though this is interesting, it really doesn't achieve what the ES6 syntax does, which is to allow you to assign a subset of the names you choose and then select the appropriate values automatically Commented Oct 23, 2023 at 10:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.