Newbie Question: Giving names to Elements of List/Tuple/Dict

holger krekel pyth at devel.trillke.net
Sat Nov 30 08:41:02 EST 2002


Oren Tirosh wrote:
> On Fri, Nov 29, 2002 at 04:01:15PM +0100, holger krekel wrote:
> > Additionally using
> > 
> >     class Struct:
> >         def __init__(self, **kw):
> >             self.__dict__.update(kw)
> > 
> > is also a nice idiom. You can then say
> > 
> > >>> one = Struct(name='guido', phone='5555')
> > >>> one.name
> > 'guido'
> > >>> one.phone
> > '5555'
> 
> If you want something even fancier, try this:
> 
> class record(dict):
>     def __init__(self, initfrom=(), **kw):
>         dict.__init__(self, initfrom)

But this pollutes the instance's namespace. 
Is inheriting from the dict class really neccessary?

>         self.update(kw)
>         self.__dict__ = self

So overall i think it's a neat trick but isn't
worth the namespace pollution. 

    holger




More information about the Python-list mailing list