[Python-Dev] Re: Patch review: [ 1094542 ] add Bunch type to
collections module
Fernando Perez
fperez.net at gmail.com
Thu Jan 27 09:07:06 CET 2005
Hi all,
Steven Bethard 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.
>>
>>
http://sourceforge.net/tracker/?func=detail&aid=1100942&group_id=5470&atid=305470
>
> I believe the correct tracker is:
>
>
http://sourceforge.net/tracker/index.php?func=detail&aid=1094542&group_id=5470&atid=305470
A while back, when I started writing ipython, I had to write this same class (I
called it Struct), and I ended up building a fairly powerful one for handling
ipython's reucursive configuration system robustly.
The design has some nasty problems which I'd change if I were doing this today
(I was just learning the language at the time). But it also taught me a few
things about what one ends up needing from such a beast in complex situations.
I've posted the code here as plain text and syntax-highlighted html, in case
anyone is interested:
http://amath.colorado.edu/faculty/fperez/python/Struct.py
http://amath.colorado.edu/faculty/fperez/python/Struct.py.html
One glaring problem of my class is the blocking of dictionary method names as
attributes, this would have to be addressed differently.
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'.
Another useful feature of this Struct class is the 'merge' method. While mine
is probably overly flexible and complex for the stdlib (though it is
incredibly useful in many situations), I'd really like dicts/Structs to have
another way of updating with a single method, which was non-destructive
(update automatically overwrites with the new data). Currently this is best
done with a loop, but a 'merge' method which would work like 'update', but
without overwriting would be a great improvement, I think.
Finally, my values() method allows an optional keys argument, which I also
find very useful. If this keys sequence is given, values are returned only
for those keys. I don't know if anyone else would find such a feature useful,
but I do :). It allows a kind of 'slicing' of dicts which can be really
convenient.
I understand that my Struct is much more of a dict/Bunch hybrid than what you
have in mind. But in heavy usage, I quickly realized that at least having
__getitem__ implemented was an immediate need in many cases.
Finally, the Bunch class should have a way of returning its values easily as a
plain dictionary, for cases when you want to pass this data into a function
which expects a true dict. Otherwise, it will 'lock' your information in.
I really would like to see such a class in the stdlib, as it's something that
pretty much everyone ends up rewriting. I certainly don't claim my
implementation to be a good reference (it isn't). But perhaps it can be
useful to the discussion as an example of a 'battle-tested' such class, flaws
and all.
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.
Regards,
More information about the Python-Dev
mailing list