watching mutables?

Mark McEahern marklists at mceahern.com
Fri Sep 27 18:17:18 EDT 2002


[I apologize for not providing context, but I've already deleted my copy of
the specific message I'm replying to.]

The reason a metaclass solution is not possible without modifying the
original code has to do with how the metaclass is determined:

  http://www.python.org/2.2.1/descrintro.html#metaclasses

  1.  First, Python looks for '__metaclass__' within the class' dict.
  2.  Python uses the metaclass of any base class.
  3.  Python looks for a global __metaclass__.
  4.  Python uses the classic metaclass types.ClassType.

With builtin lists and dicts, Python never gets past step 2, so you have to
intervene forcefully with step 1 if you want to override the metaclass of a
builtin class, which means you have to first subclass it:

  class WatchedDict(dict):

    __metaclass__ = Watched

// m





More information about the Python-list mailing list