pre-PEP generic objects

Nick Craig-Wood nick at craig-wood.com
Tue Nov 30 11:30:03 CET 2004


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!

This sounds very much like this class which I've used to convert perl
programs to python

class Hash:
    def __init__(self, **kwargs):
        for key,value in kwargs.items():
            setattr(self, key, value)
    def __getitem__(self, x):
        return getattr(self, x)
    def __setitem__(self, x, y):
        setattr(self, x, y)

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.

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.

YMMV ;-)

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



More information about the Python-list mailing list