__dict__s and types and maybe metaclasses...

Discussion in 'Python' started by Adam Atlas, May 2, 2007.

  1. Adam Atlas

    Adam Atlas Guest

    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.
     
    Adam Atlas, May 2, 2007
    #1
    1. Advertising

  2. Adam Atlas

    Larry Bates Guest

    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_values={'a': 123, 'b': 456}

    def __init__(self):
    self.__dict__.update(self.__initialvalues)

    -Larry
     
    Larry Bates, May 2, 2007
    #2
    1. Advertising

  3. Adam Atlas

    Adam Atlas Guest

    On May 2, 5:24 pm, Larry Bates <> wrote:
    > I think that most people accomplish this by:
    >
    > class blah:
    > __initial_values={'a': 123, 'b': 456}
    >
    > def __init__(self):
    > self.__dict__.update(self.__initialvalues)


    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.
     
    Adam Atlas, May 3, 2007
    #3
  4. 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__(self, key):
    ...
    def __setitem__(self, key, value):
    ...

    ... magic incantation to use a MyDict instance for class Foo ...
    class Foo(object):
    a = 1 # calls MyDict.__setitem__('a', 1)
    def bar(self): # calls MyDict.__setitem__('bar', <func>)
    ...
    b = a + 2 # calls MyDict.__getitem__('a') and then
    # calls MyDict.__setitem__('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
     
    Steven Bethard, May 3, 2007
    #4
  5. Adam Atlas <> wrote:

    > On May 2, 5:24 pm, Larry Bates <> wrote:
    > > I think that most people accomplish this by:
    > >
    > > class blah:
    > > __initial_values={'a': 123, 'b': 456}
    > >
    > > def __init__(self):
    > > self.__dict__.update(self.__initialvalues)

    >
    > 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 <body> to 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
     
    Alex Martelli, May 3, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    5
    Views:
    639
  2. Santiago Aguiar

    AOP and metaclasses

    Santiago Aguiar, Feb 20, 2004, in forum: Python
    Replies:
    2
    Views:
    338
    John Roth
    Feb 20, 2004
  3. Mark Nottingham

    metaclasses and Exceptions

    Mark Nottingham, Jul 29, 2004, in forum: Python
    Replies:
    6
    Views:
    330
    Michael Hudson
    Aug 3, 2004
  4. Ville Vainio
    Replies:
    11
    Views:
    635
    Hamish Lawson
    Aug 10, 2004
  5. Steve Menard

    Mixing metaclasses and exceptions

    Steve Menard, Dec 28, 2004, in forum: Python
    Replies:
    2
    Views:
    430
    Alex Martelli
    Dec 31, 2004
Loading...

Share This Page