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 an

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): > > &

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
bout it, I think this might fall under 'we're all > consenting adults here'. I mean, you can shadow the dict constructor > from the toplevel if you're so inclined (or don't know any better). > But you could make it harder to accidentally shadow builtin

Re: class attrdict

2007-03-03 Thread MonkeeSage
adults here'. I mean, you can shadow the dict constructor from the toplevel if you're so inclined (or don't know any better). But you could make it harder to accidentally shadow builtin attributes: from warnings import warn class attrdict(dict): __builtins__ = dir(dict)

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 = "&#x

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 Attribut

Re: class attrdict

2007-03-02 Thread Alex Martelli
], making the whole program extremely fragile -- a very high > > price to pay for some modest amount of syntax sugar. > > How about something like... > > class attrdict(dict): > def __init__(self, *args, **kwargs): > dict.__init__(self, *args, **kwargs) >

Re: class attrdict

2007-03-02 Thread MonkeeSage
price to pay for some modest amount of syntax sugar. How about something like... class attrdict(dict): def __init__(self, *args, **kwargs): dict.__init__(self, *args, **kwargs) for k, v in self.items(): dict.__setattr__(self, str(k), v) def __setitem__(sel

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""" &g

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 __

class attrdict

2007-03-02 Thread Hallvard B Furuseth
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