Metaclass with name overloading.

Alex Martelli aleaxit at yahoo.com
Mon Sep 27 08:52:11 EDT 2004


Jacek Generowicz <jacek.generowicz at cern.ch> wrote:
   ...
> I would like to write a metaclass which would allow me to overload
> names in the definition of its instances, like this
   ...
> I was wondering whether it would be possible to achieve this by
> forcing Python to use some dicitonary proxy (which accumulates values,
> rather that keeping just the last value to be associated with a key),
> instead of dict, when executing the class definiton?
> 
> Is something like this at all possible in pure Python? or does in
> require fiddling around in the guts of the parser?

It's not possible in pure Python -- Python will always use a real dict
rather than any other type as you'd wish.  The parser OTOH shouldn't
really be involved, either.  In the end it boils down to a series of
STORE_NAME pseudocode instructions, which currently do:

                case STORE_NAME:
                        w = GETITEM(names, oparg);
                        v = POP();
                        if ((x = f->f_locals) != NULL) {
                                if (PyDict_CheckExact(x))
                                        err = PyDict_SetItem(x, w, v);
                                else
                                        err = PyObject_SetItem(x, w, v);
                                Py_DECREF(v);

so they'd be able to work with f_locals being either a dict, or any
other mapping -- so that part should be OK.  But having frame f's
f_locals be anything but a dict, now THAT is the problem...


Alex




More information about the Python-list mailing list