__dict__s and types and maybe metaclasses...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adam Atlas

    #1

    __dict__s and types and maybe metaclasses...

    Suppose I want to create a type (i.e. a new-style class via the usual
    `class blah(...)` mechanism) but, during the process of creating the
    type, I want to replace its __dict__ so I can override some behaviors
    during the initial assignment of its members. That is, I have `class
    blah(...): a = 123; b = 456; ...`, and I want to substitute my own
    dict subclass which will thus receive __setitem__(a, 123),
    __setitem__(b, 456), and so on.

    Is this possible? Maybe with metaclasses? I've experimented with them
    a bit, but I haven't found any setup that works.

  • Larry Bates

    #2
    Re: __dict__s and types and maybe metaclasses...

    Adam Atlas wrote:
    Suppose I want to create a type (i.e. a new-style class via the usual
    `class blah(...)` mechanism) but, during the process of creating the
    type, I want to replace its __dict__ so I can override some behaviors
    during the initial assignment of its members. That is, I have `class
    blah(...): a = 123; b = 456; ...`, and I want to substitute my own
    dict subclass which will thus receive __setitem__(a, 123),
    __setitem__(b, 456), and so on.
    >
    Is this possible? Maybe with metaclasses? I've experimented with them
    a bit, but I haven't found any setup that works.
    >
    I think that most people accomplish this by:

    class blah:
    __initial_value s={'a': 123, 'b': 456}

    def __init__(self):
    self.__dict__.u pdate(self.__in itialvalues)

    -Larry

    Comment

    • Adam Atlas

      #3
      Re: __dict__s and types and maybe metaclasses...

      On May 2, 5:24 pm, Larry Bates <larry.ba...@we bsafe.comwrote:
      I think that most people accomplish this by:
      >
      class blah:
      __initial_value s={'a': 123, 'b': 456}
      >
      def __init__(self):
      self.__dict__.u pdate(self.__in itialvalues)
      That's not really what I'm talking about... I'm talking about
      replacing the __dict__ with a SUBCLASS of dict (not just default
      values), and that's at the time of the class declaration (the creation
      of `blah` as a `type` instance), not at instance creation.

      Comment

      • Steven Bethard

        #4
        Re: __dict__s and types and maybe metaclasses...

        Adam Atlas wrote:
        Suppose I want to create a type (i.e. a new-style class via the usual
        `class blah(...)` mechanism) but, during the process of creating the
        type, I want to replace its __dict__
        If I understand you right, what you want is something like::

        class MyDict(object):
        def __getitem__(sel f, key):
        ...
        def __setitem__(sel f, key, value):
        ...

        ... magic incantation to use a MyDict instance for class Foo ...
        class Foo(object):
        a = 1 # calls MyDict.__setite m__('a', 1)
        def bar(self): # calls MyDict.__setite m__('bar', <func>)
        ...
        b = a + 2 # calls MyDict.__getite m__('a') and then
        # calls MyDict.__setite m__('b', 3)

        If that's right, then the answer is "no, you can't do this". There was
        some discussion of allowing such a thing in Python 3.0, but it fizzled.
        (Check the python-3000 archives if you're interested.)


        So what's the real problem you're trying to solve?

        STeVe

        Comment

        • Alex Martelli

          #5
          Re: __dict__s and types and maybe metaclasses...

          Adam Atlas <adam@atlas.stw rote:
          On May 2, 5:24 pm, Larry Bates <larry.ba...@we bsafe.comwrote:
          I think that most people accomplish this by:

          class blah:
          __initial_value s={'a': 123, 'b': 456}

          def __init__(self):
          self.__dict__.u pdate(self.__in itialvalues)
          >
          That's not really what I'm talking about... I'm talking about
          replacing the __dict__ with a SUBCLASS of dict (not just default
          values), and that's at the time of the class declaration (the creation
          of `blah` as a `type` instance), not at instance creation.
          The metaclass only enters the picture AFTER the class body has run (and
          built a __dict__). As I explained many times:

          class ic(bases):
          <body>

          means:

          -- run the <bodyto create a dictionary D
          -- identify the metaclass M
          -- execute:
          ic = M("ic", bases, D)

          the body always creates a dictionary -- you can't override that with
          metaclasses. Maybe some deep bytecode hacks might work, but I have some
          doubts in the matter (not given it much thought, tho).


          Alex

          Comment

          Working...