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

Path csiph.com!eternal-september.org!feeder.eternal-september.org!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <chris@rebertia.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'url:pypi': 0.03; '(self,': 0.07; 'pypi': 0.07; 'cc:addr:python-list': 0.09; '(instead': 0.09; '__init__': 0.09; 'immutable': 0.09; 'kwargs': 0.09; 'type:': 0.09; 'python': 0.10; 'syntax': 0.13; 'def': 0.13; 'instead.': 0.15; 'producing': 0.15; 'cc:name:python': 0.16; 'drag': 0.16; 'libraries?': 0.16; 'subject:object': 0.16; 'wrote:': 0.16; 'attribute': 0.18; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'aug': 0.20; 'class,': 0.22; 'cheers,': 0.22; 'passing': 0.23; 'import': 0.24; 'plain': 0.24; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'module': 0.25; 'chris': 0.26; 'message-id:@mail.gmail.com': 0.27; '**kwargs):': 0.29; 'dictionary': 0.29; 'subject:/': 0.30; 'code': 0.30; "i'd": 0.31; 'fixed': 0.31; 'knows': 0.32; 'anybody': 0.32; 'class': 0.33; 'url:python': 0.33; 'curious': 0.33; 'subject:like': 0.33; 'similar': 0.33; 'except': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'functions.': 0.35; 'something': 0.35; 'but': 0.36; 'located': 0.36; 'there': 0.36; 'url:org': 0.36; 'data.': 0.36; 'url:library': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'client': 0.37; 'version': 0.38; 'names': 0.38; 'hi,': 0.38; 'data': 0.39; 'subject:-': 0.39; 'url:3': 0.60; 'side': 0.62; 'between': 0.65; 'believe': 0.66; 'overall': 0.72; 'sender:addr:chris': 0.84; 'glad': 0.87; 'subject:looking': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; bh=cTGZ3yLHwYri4lUcRiqIECFjPCCd6kkj/oGwz8AwZvk=; b=RAbE3wMNMEXNQ0Yjv3WKCP8md0iVnEt+hoe9E5OJPqF0PJDqICnEcVkgmfKL2oNAaE RDPrEWqR5uaDlChPO4AzEd/fTFGUFIQCMEnJFqw7m82Qj1xVl6U9o66ehfpmaAX38fQa fG4cpTSyN8GTMlfXWeHXfF/gyqb40PrvopmX0=
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:sender:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=cTGZ3yLHwYri4lUcRiqIECFjPCCd6kkj/oGwz8AwZvk=; b=hddUZ4PZnAIsB4ADMwLpX/OkK9Hpr+2MBLul7Im4nqQZVnsDSc67nJB6iAnSlir6Yn efuq50ub3HBJ5YykLbOD4E2dkMuBVz/oPjdkd0Rnmv8RYoIi1+A+crKr0LzIkaqSll8B ZZskXXtiCHzcthN992GsnyWhkUm67IZ/0Ga0HOqsecnMhyi5+vVuSAfnZ5GE4+n7wUgw QuPNNFqTKJaIJmOVwm/2ezSg1AhSshkQaBSg/hCF9Vynju/R7gSsHYCcJ705OEojycrH hwPyHz8UcmWF/eGma+lV41Ln6ZxODDZolatedx8oAqKrvU6kh5ByhDqCwTvgj2xdw8XI HZmw==
X-Gm-Message-State ALoCoQmkxjNHi+KLoaRm8bsIdU33QqQJE2WRUL3L0BmDkH+B9dW/zhpeuv3Jayz4cW0HwbvXerPz
MIME-Version 1.0
X-Received by 10.50.27.39 with SMTP id q7mr14520965igg.73.1439264160284; Mon, 10 Aug 2015 20:36:00 -0700 (PDT)
Sender chris@rebertia.com
In-Reply-To <CAK_Erkid-bWZpUhmLsBmRvDPW2iyQG-RHLRq4LrgJozoC6tMvQ@mail.gmail.com>
References <CAK_Erkid-bWZpUhmLsBmRvDPW2iyQG-RHLRq4LrgJozoC6tMvQ@mail.gmail.com>
Date Mon, 10 Aug 2015 20:36:00 -0700
X-Google-Sender-Auth _LhL8kCir5-dxT15POFuftmLYjw
Subject Re: looking for standard/builtin dict-like data object
From Chris Rebert <clp2@rebertia.com>
To Vladimir Ignatov <kmisoft@gmail.com>
Cc Python <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.61.1439264168.3627.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1439264168 news.xs4all.nl 2825 [2001:888:2000:d::a6]:52204
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:95241

Show key headers only | 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