Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > __dict__s and types and maybe metaclasses...

Reply
Thread Tools

__dict__s and types and maybe metaclasses...

 
 
Adam Atlas
Guest
Posts: n/a
 
      05-02-2007
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.

 
Reply With Quote
 
 
 
 
Larry Bates
Guest
Posts: n/a
 
      05-02-2007
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
 
Reply With Quote
 
 
 
 
Adam Atlas
Guest
Posts: n/a
 
      05-02-2007
On May 2, 5:24 pm, Larry Bates <(E-Mail Removed)> 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.

 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      05-02-2007
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
 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      05-03-2007
Adam Atlas <(E-Mail Removed)> wrote:

> On May 2, 5:24 pm, Larry Bates <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Argument type of function and safe types and types, arguments canhandle safely ittium C++ 4 12-09-2011 11:05 AM
Maybe it will; maybe it won't iL_weReo Computer Support 7 10-13-2009 08:42 AM
Of Intrest ? Maybe Yes, Maybe No Old Gringo Computer Support 0 10-11-2004 01:35 AM
Maybe, just maybe @decorator syntax is ok after all Ville Vainio Python 11 08-10-2004 06:01 PM
Some concepts (Maybe LOL maybe not...) C++ 5 08-18-2003 05:48 PM



Advertisments