pre-PEP generic objects

Nick Craig-Wood nick at craig-wood.com
Thu Dec 2 01:46:45 EST 2004


Steven Bethard <steven.bethard at gmail.com> wrote:
>  Nick Craig-Wood wrote:
> > Steven Bethard <steven.bethard at gmail.com> 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!
> > 
> > My experience from using this is that whenever I used Hash(), I found
> > that later on in the refinement of the conversion it became its own
> > class.
> 
>  This has also generally been my experience, though I'm not sure it's as 
>  true for the XML DOM to Bunch translation.  Did you use Hash() in the 
>  same way for hierarchical data?

Hash() got nested yes, but not in a general purpose structure like
your XML example.
> 
> > So my take on the matter is that this encourages perl style
> > programming (just ram it in a hash, and write lots of functions acting
> > on it) rather than creating a specific class for the job which is dead
> > easy in python anyway and to which you can attach methods etc.
> 
>  You'll note that the (pre-)PEP explicitly notes that this object is 
>  intended only for use when no methods are associated with the attributes:
> 
>  "When no methods are to be associated with the attribute-value mappings,
>  declaring a new class can be overkill."
> 
>  I do understand your point though -- people might not use Bunch in the 
>  way it's intended.  Of course, those same people can already do the same 
>  thing with a dict instead (e.g. write a bunch of functions to handle a 
>  certain type of dict).  If someone wants to write Perl in Python, 
>  there's not much we can really do to stop them...

No there isn't ;-)

The above does make it a lot more convenient though blob['foo'] is
rather difficult to type compared to blob.foo!

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list