Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #95241

Re: looking for standard/builtin dict-like data object

References <CAK_Erkid-bWZpUhmLsBmRvDPW2iyQG-RHLRq4LrgJozoC6tMvQ@mail.gmail.com>
Date 2015-08-10 20:36 -0700
Subject Re: looking for standard/builtin dict-like data object
From Chris Rebert <clp2@rebertia.com>
Newsgroups comp.lang.python
Message-ID <mailman.61.1439264168.3627.python-list@python.org> (permalink)

Show all headers | View raw


On Mon, Aug 10, 2015 at 8:22 PM, Vladimir Ignatov <kmisoft@gmail.com> wrote:
> Hi,
>
> In my code I often use my own home-brewed object for passing bunch of
> data between functions. Something like:
>
> class Data(object):
>     def __init__ (self, **kwargs):
>         self.__dict__ = kwargs
>
> ....
>
> return Data(attr1=..., attr2=..., attr3=...)
>
> Logically it works like plain dictionary but with nice result.attrX
> syntax on client side (instead of resut['attrX']).   Overall I am
> pretty happy with this approach except that I need to drag Data class
> around my projects and import its module in every code producing data.
>
> I am curious if anybody knows similar "dummy" class located in
> standard libraries? I'd be glad to use it instead.

This is commonly known as a "Bunch" class, after the Python Cookbook
recipe. I don't believe there's any public version of it in the std
lib, but there is a PyPI package for it:
https://pypi.python.org/pypi/bunch

However, if the set of attribute names is fixed and an immutable
datatype is acceptable, you could use the std lib's "namedtuple" type:
https://docs.python.org/3/library/collections.html#collections.namedtuple

Cheers,
Chris
--
http://chrisrebert.com

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: looking for standard/builtin dict-like data object Chris Rebert <clp2@rebertia.com> - 2015-08-10 20:36 -0700

csiph-web