By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,898 Members | 1,198 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,898 IT Pros & Developers. It's quick & easy.

__dict__s and types and maybe metaclasses...

P: n/a
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.

May 2 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
May 2 '07 #2

P: n/a
On May 2, 5:24 pm, Larry Bates <larry.ba...@websafe.comwrote:
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.

May 2 '07 #3

P: n/a
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
May 2 '07 #4

P: n/a
Adam Atlas <ad**@atlas.stwrote:
On May 2, 5:24 pm, Larry Bates <larry.ba...@websafe.comwrote:
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 <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
May 3 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.