From ebbda7eb59052b12bff8270de347fb84a086fe9e Mon Sep 17 00:00:00 2001 From: Adam Parkin Date: Fri, 25 Aug 2023 10:13:52 -0700 Subject: [PATCH] Post on using namedtuples to convert dicts to objects --- .../ptotd-namedtuple-for-dict-to-object.md | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 content/ptotd-namedtuple-for-dict-to-object.md diff --git a/content/ptotd-namedtuple-for-dict-to-object.md b/content/ptotd-namedtuple-for-dict-to-object.md new file mode 100644 index 0000000..82d5a33 --- /dev/null +++ b/content/ptotd-namedtuple-for-dict-to-object.md @@ -0,0 +1,33 @@ +Title: Using namedtuple's to convert dicts to objects +Date: 2023-08-25 15:06 +Modified: 2023-08-25 15:06 +Category: Posts +tags: python,dict,namedtuple +cover: static/imgs/python-logo-master-v3-TM.png +summary: Quick and dirty way to convert a dict into an object in Python using namedtuple's + +A friend shared this with me today and I thought it was pretty neat. If you have a dict, and +you want to convert it to an object where the object properties are the keys from the dict, and +the values are the values from the dict, you can use a namedtuple to do so. For example: + +```python +>>> some_dict = {"name": "My name", "func" : "my func"} +>>> import namedtuple +>>> SomeClass = namedtuple("SomeClass", some_dict.keys()) +>>> as_an_object = SomeClass(**some_dict) +>>> as_an_object.name +'My name' +>>> as_an_object.func +'my func' +``` + +Won't handle nested dicts (the sub-dicts will still be dicts on the constructed object), but +for a quick and dirty way to convert a dict to an object, this seems pretty handy. + +Using the splat operator you can also save a line of code: + +```python +>>> as_an_object = namedtuple("SomeClass", some_dict.keys())(**some_dict) +>>> as_an_object +SomeClass(name='My name', func='my func') +```