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


Groups > comp.lang.python > #95242 > unrolled thread

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

Started byCameron Simpson <cs@zip.com.au>
First post2015-08-11 13:40 +1000
Last post2015-08-11 13:40 +1000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: looking for standard/builtin dict-like data object Cameron Simpson <cs@zip.com.au> - 2015-08-11 13:40 +1000

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

FromCameron Simpson <cs@zip.com.au>
Date2015-08-11 13:40 +1000
SubjectRe: looking for standard/builtin dict-like data object
Message-ID<mailman.62.1439264463.3627.python-list@python.org>
On 10Aug2015 23:22, Vladimir Ignatov <kmisoft@gmail.com> wrote:
>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've got a base class called "O" like that:
  https://pypi.python.org/pypi/cs.obj/

>I am curious if anybody knows similar "dummy" class located in
>standard libraries? I'd be glad to use it instead.

namedtuple initialises and accesses like that:

    >>> from collections import namedtuple
    >>> Klass = namedtuple('Klass', 'a b c')
    >>> o1 = Klass(1,2,3)
    >>> o1
    O(a=1, b=2, c=3)
    >>> o1.b
    2
    >>> o2 = Klass(a=1,c=3,b=2)
    >>> o2
    O(a=1, b=2, c=3)

namedtuple makes a factory for making particular flavours.
And the result us a tuple, not a dict.

I also thought the stdlib had some kind of "namespace" class with this kind of 
API, but I can't find it now:-(

Cheers,
Cameron Simpson <cs@zip.com.au>

Always code as if the guy who ends up maintaining your code will be a violent
psychopath who knows where you live.
        - Martin Golding, DoD #0236, martin@plaza.ds.adp.com

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web