Re: class attrdict

2007-03-09 Thread Hallvard B Furuseth
Alex Martelli writes: > You make a good point. I do like being able to say foo.bar=baz rather > than foo['bar']=baz in certain cases -- not so much to save 3 chars, but > to avoid excessive punctuation; however, I don't really need this AND > all of dict's power at the same time, so, I don't inher

Re: class attrdict

2007-03-09 Thread Hallvard B Furuseth
Alex Martelli writes: > (...) >> class Namespace(object): > (...) > I might, if it weren't for the redundant "if" and the horribly buggy > interference between separate instances -- which is why I wrote it, > almost six years ago and without the bugs, as >

Re: class attrdict

2007-03-04 Thread goodwolf
class Namespace(object): def __init__(self, __ns=None, **kwargs): if __ns is None:#if no dictionary is given self.__dict__ = kwargs #then use kwargs without copying or creating new dict else: assert len(kwargs) == 0 self.__dict__

Re: class attrdict

2007-03-04 Thread Alex Martelli
goodwolf <[EMAIL PROTECTED]> wrote: ... > Then you will prefer something like this: > > class Namespace(object): > def __init__(self, __ns={}, **kwargs): > if kwargs: __ns.update(kwargs) > self.__dict__ = __ns I might, if it weren't for the redundant "if" and the horribly b

Re: class attrdict

2007-03-04 Thread goodwolf
On Mar 4, 1:03 pm, "goodwolf" <[EMAIL PROTECTED]> wrote: > On Mar 3, 4:25 am, [EMAIL PROTECTED] (Alex Martelli) wrote: > > > > > Hallvard B Furuseth <[EMAIL PROTECTED]> wrote: > > > > Does this class need anything more? > > > Is there any risk of a lookup loop? > > > Seems to work... > > > > class

Re: class attrdict

2007-03-04 Thread goodwolf
On Mar 3, 4:25 am, [EMAIL PROTECTED] (Alex Martelli) wrote: > Hallvard B Furuseth <[EMAIL PROTECTED]> wrote: > > > Does this class need anything more? > > Is there any risk of a lookup loop? > > Seems to work... > > > class attrdict(dict): > > """Dict where d['foo'] also can be accessed as d.fo

Re: class attrdict

2007-03-03 Thread Alex Martelli
MonkeeSage <[EMAIL PROTECTED]> wrote: > On Mar 3, 7:54 pm, [EMAIL PROTECTED] (Alex Martelli) wrote: > > Besides missing the warning in the __init__, this also has pretty weird > > behavior whenever subject to a .pop, .update, .setdefault, del of either > > item or attr, etc, etc: the attributes an

Re: class attrdict

2007-03-03 Thread MonkeeSage
On Mar 3, 7:54 pm, [EMAIL PROTECTED] (Alex Martelli) wrote: > Besides missing the warning in the __init__, this also has pretty weird > behavior whenever subject to a .pop, .update, .setdefault, del of either > item or attr, etc, etc: the attributes and items "get out of sync" (and, > catching each

Re: class attrdict

2007-03-03 Thread Alex Martelli
MonkeeSage <[EMAIL PROTECTED]> wrote: > On Mar 3, 1:29 pm, [EMAIL PROTECTED] (Alex Martelli) wrote: > > It would be nice, yes, weren't it for the inevitable irregularity, one > > way or another, caused by the clash between attributes and items. > > In thinking about it, I think this might fall un

Re: class attrdict

2007-03-03 Thread MonkeeSage
On Mar 3, 1:29 pm, [EMAIL PROTECTED] (Alex Martelli) wrote: > It would be nice, yes, weren't it for the inevitable irregularity, one > way or another, caused by the clash between attributes and items. In thinking about it, I think this might fall under 'we're all consenting adults here'. I mean, y

Re: class attrdict

2007-03-03 Thread Alex Martelli
Andrew Coffman <[EMAIL PROTECTED]> wrote: > Could you do something like this? > > class attrdict(dict): > def __getattr__(self, attr): > if self.has_key(attr): > return self[attr] > else: > message = "'attrdict' object has no attribute '%s'" % attr

Re: class attrdict

2007-03-03 Thread Andrew Coffman
Could you do something like this? class attrdict(dict): def __getattr__(self, attr): if self.has_key(attr): return self[attr] else: message = "'attrdict' object has no attribute '%s'" % attr raise AttributeError, message If you have a

Re: class attrdict

2007-03-02 Thread Alex Martelli
MonkeeSage <[EMAIL PROTECTED]> wrote: > On Mar 2, 9:25 pm, [EMAIL PROTECTED] (Alex Martelli) wrote: > > The problem is mostly that, given an instance a of attrdict, whether you > > can call (e.g.) a.update(foo) depends on whether you ever set > > a['update'], making the whole program extremely fra

Re: class attrdict

2007-03-02 Thread MonkeeSage
On Mar 2, 9:25 pm, [EMAIL PROTECTED] (Alex Martelli) wrote: > The problem is mostly that, given an instance a of attrdict, whether you > can call (e.g.) a.update(foo) depends on whether you ever set > a['update'], making the whole program extremely fragile -- a very high > price to pay for some mod

Re: class attrdict

2007-03-02 Thread Alex Martelli
Hallvard B Furuseth <[EMAIL PROTECTED]> wrote: > Does this class need anything more? > Is there any risk of a lookup loop? > Seems to work... > > class attrdict(dict): > """Dict where d['foo'] also can be accessed as d.foo""" > def __init__(self, *args, **kwargs): > self.__dict__

Re: class attrdict

2007-03-02 Thread James Stroud
Hallvard B Furuseth wrote: > Does this class need anything more? > Is there any risk of a lookup loop? > Seems to work... > > class attrdict(dict): > """Dict where d['foo'] also can be accessed as d.foo""" > def __init__(self, *args, **kwargs): > self.__dict__ = self > dict