__dict__s and types and maybe metaclasses...

A

Adam Atlas

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.
 
L

Larry Bates

Adam said:
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
 
A

Adam Atlas

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.
 
S

Steven Bethard

Adam said:
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
 
A

Alex Martelli

Adam Atlas said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top