473,387 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

May 2 '07 #1
4 931
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Stephan Diehl | last post by:
I have a question about metaclasses: How would be the best way to "merge" different metaclasses? Or, more precisely, what is the best way to merge metaclass functionality? The idea is basicly...
0
by: Shalabh Chaturvedi | last post by:
Announcing the initial public draft of: The New Python Part 1 - Types and Objects From the abstract: "Describes the different new-style objects, and how they are related in the new Python...
27
by: Michele Simionato | last post by:
> Hello, my name is Skip and I am metaclass-unaware. I've been programming in > Python for about ten years and I have yet to write a metaclass. At first I > thought it was just that metaclasses...
4
by: Michael Sparks | last post by:
Anyway... At Europython Guido discussed with everyone the outstanding issue with decorators and there was a clear majority in favour of having them, which was good. From where I was sitting it...
13
by: Jean-François Doyon | last post by:
Hello, I'm using MetaClasses to create classes. How do I make these new classes "globally" available? I probably just have to assign them to something magic, but I can't seem to figure out...
1
by: Hartmut Goebel | last post by:
Hi, I'm currently implementing a tool for auto-generating webforms from a desription file. The output should become a php-script, an asp-script, zope-formulator or such. Each if it may (or may...
0
by: Jan-Ole Esleben | last post by:
Hi! I've just posted a question about metaclasses in ZOPE on the ZOPE list, and one of the replies said that metaclasses (at least "painless" metaclasses) cannot be used without new-style...
5
by: venk | last post by:
Hi, can some one properly explain the differences between class types and classic classes? ... Still face problems in identifying what is what.
1
by: Alan Isaac | last post by:
Forman's book is out of print. Is there a good substitute? Thanks, Alan Isaac
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.