[Python-Dev] Re: Patch review: [ 1094542 ] add Bunch type to collections module (original) (raw)
Steven Bethard steven.bethard at gmail.com
Thu Jan 27 18:52:33 CET 2005
- Previous message: [Python-Dev] Re: PEP 309
- Next message: [Python-Dev] Re: Re: Patch review: [ 1094542 ] add Bunch type to collections module
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Fernando Perez <fperez.net at gmail.com> wrote:
> Alan Green <alan.green at gmail.com> wrote: >> Steven Bethard is proposing a new collection class named Bunch. I had >> a few suggestions which I attached as comments to the patch - but what >> is really required is a bit more work on the draft PEP, and then >> discussion on the python-dev mailing list.
But one thing which I really find necessary from a useful 'Bunch' class, is the ability to access attributes via foo[name] (which requires implementing getitem). Named access is convenient when you know the name you need (foo.attr). However, if the name of the attribute is held in a variable, IMHO foo[name] beats getattr(foo,name) in clarity and feels much more 'pythonic'.
My feeling about this is that if the name of the attribute is held in a variable, you should be using a dict, not a Bunch/Struct. If you have a Bunch/Struct and decide you want a dict instead, you can just use vars:
py> b = Bunch(a=1, b=2, c=3) py> vars(b) {'a': 1, 'c': 3, 'b': 2}
Another useful feature of this Struct class is the 'merge' method. [snip] my values() method allows an optional keys argument, which I also find very useful.
Both of these features sound useful, but I don't see that they're particularly more useful in the case of a Bunch/Struct than they are for dict. If dict gets such methods, then I'd be happy to add them to Bunch/Struct, but for consistency's sake, I think at the moment I'd prefer that people who want this functionality subclass Bunch/Struct and add the methods themselves.
I think the current pre-PEP version is a bit too limited to be generally useful in complex, real-world situtations. It would be a good starting point to subclass for more demanding situations, but IMHO it would be worth considering a more powerful default class.
I'm probably not willing to budge much on adding dict-style methods -- if you want a dict, use a dict. But if people think they're necessary, there are a few methods from Struct that I wouldn't be too upset if I had to add, e.g. clear, copy, etc. But I'm going to need more feedback before I make any changes like this.
Steve
You can wordify anything if you just verb it. --- Bucky Katt, Get Fuzzy
- Previous message: [Python-Dev] Re: PEP 309
- Next message: [Python-Dev] Re: Re: Patch review: [ 1094542 ] add Bunch type to collections module
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]