pre-PEP generic objects

Steven Bethard steven.bethard at gmail.com
Thu Dec 2 19:46:09 EST 2004


Istvan Albert wrote:
> Steven Bethard wrote:
> 
>> I promised I'd put together a PEP for a 'generic object' data type for 
>> Python 2.5 that allows one to replace __getitem__ style access with 
>> dotted-attribute style access (without declaring another class).  Any 
>> comments would be appreciated!
> 
> 
> IMHO this too easy to accomplish right now to warrant
> an "official" implementation:
> 
> class Bunch:
>     pass
> 
> b = Bunch()
> b.one, b.two, b.three = 1,2,3
> 
> works just fine, depending on the problem I might add a few special
> operators. For anything more complicated I'd rather write a real class.

You'll note that my implementation really isn't much more than this.  (A 
little bit extra to make converting hierarchies easier.)  The question 
is not how easy it is to write, but how many times it's going to get 
written.  If you're going to write your 2-line Bunch class (which should 
probably be the 3-line Bunch class that uses new-style classes) a 
thousand times, I think including a class that does this for you (and 
provides a few other nice properties) is a Good Thing.  If you're only 
ever going to use it once, then yes, there's probably no reason to 
include it in the stdlib.

The belief that I gathered from the end of the previous thread 
discussing this (check last week's python-list I think) was that there 
were a significant number of people who had wanted a class like this 
(notably IPython), and more than one of them had rewritten the class a 
few times.

Steve



More information about the Python-list mailing list