473,387 Members | 1,465 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.

Looking for magic method to override to prevent dict(d) from grabbing subclass inst d contents directly

Has anyone found a way besides not deriving from dict?
Shouldn't there be a way?
TIA
(need this for what I hope is an improvement on the Larosa/Foord OrderedDict ;-)

I guess I can just document that you have to spell it dict(d.items()), but I'd
like to hide the internal shenanigans ;-)

Regards,
Bengt Richter
Nov 22 '05 #1
3 2031
bo**@oz.net (Bengt Richter) writes:
Has anyone found a way besides not deriving from dict?
Shouldn't there be a way?
TIA
(need this for what I hope is an improvement on the Larosa/Foord OrderedDict ;-)

I guess I can just document that you have to spell it dict(d.items()), but I'd
like to hide the internal shenanigans ;-)


So why not just hide them:
class md(dict):

.... def __new__(cls, arg):
.... return dict.__new__(cls, arg.items())
I'm not sure exactly what you mean by "grabbing sublass inst d contens
directly", but if dict(d.items()) does it, the above class should do
it as well. Of course, *other* ways of initializing a dict won't work
for this class.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Nov 22 '05 #2
On Tue, 22 Nov 2005 09:30:49 -0500, Mike Meyer <mw*@mired.org> wrote:
bo**@oz.net (Bengt Richter) writes:
Has anyone found a way besides not deriving from dict?
Shouldn't there be a way?
TIA
(need this for what I hope is an improvement on the Larosa/Foord OrderedDict ;-)

I guess I can just document that you have to spell it dict(d.items()), but I'd
like to hide the internal shenanigans ;-)


So why not just hide them:
class md(dict):... def __new__(cls, arg):
... return dict.__new__(cls, arg.items())
I'm not sure exactly what you mean by "grabbing sublass inst d contens
directly", but if dict(d.items()) does it, the above class should do
it as well. Of course, *other* ways of initializing a dict won't work
for this class.

It's not initializing the dict subclass that is a problem, it is passing
it to the plain dict builtin as a constructor argument and being able to
control what is pulled from the subclass object to do the construction
of the ordinary dict.

Here's where I am on this: (odictb is my version of odict ;-)
from odictb import OrderedDict
d = OrderedDict()
d['a'] = 1
d['b'] = 2
d {'a': 1, 'b': 2} dict(d.items()) {'a': 1, 'b': 2}

Looks normal, but I am secretly using key:(insertionseqno, value) to represent key:value
dict(d)

{'a': (0, 1), 'b': (1, 2)}

and I want to control dict(d) that so the result is like dict(d.items()) above.

The dict builtin apparently skips looking for methods in the dict subclass. If it were
a subclass of object, dict would probably be looking for __iter__ or __getitem__ or __???___
but with a dict subclass it seems to bypass such a check. I was hoping that even so
there might be a __???__ that I didn't know about.

[Oop, just discovered a bug in my implementation ;-/ Hope it doesn't ripple...]

Regards,
Bengt Richter
Nov 22 '05 #3
bo**@oz.net (Bengt Richter) writes:
I'm not sure exactly what you mean by "grabbing sublass inst d contens
directly", but if dict(d.items()) does it, the above class should do
it as well. Of course, *other* ways of initializing a dict won't work
for this class.

It's not initializing the dict subclass that is a problem, it is passing
it to the plain dict builtin as a constructor argument and being able to
control what is pulled from the subclass object to do the construction
of the ordinary dict.


Ah, I got it backwards.

In looking through the dict sources in 2.4.2, it looks like the
initializer creates the new dictionary, then calls the C update for
dictionary it was passed. Update checks to see if the "other" is a
real dictionary, and if so assumes that it can just use the C code for
a dict for performance reasons. So there's no way to do what you're
trying to do. You either have to not pass the dictionary, or not
subclass dictionary. You might subclass UserDict.

IIRC, there are other places where this kind of thing happens:
specifically with files before.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Nov 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Matjaz | last post by:
Dear all, I have trouble with creating objects with Python C API, subclassed from dict type. What I am trying to do is to subclass a dict class (in Python) and create its instance in a Python...
12
by: Donnal Walter | last post by:
The following method is defined in one of my classes: def setup(self, items={}): """perform setup based on a dictionary of items""" if 'something' in items: value = items # now do something...
4
by: Phill | last post by:
I read in one of Jesse Liberty's books that: "Because ToString() is a virtual method in the base class Object, it is guaranteed to be available in every derived class." (P.179 Programming C#) ...
2
by: Polo | last post by:
Hi I have a abstract class and some other than inherite from it I woul like to return a bitmap (defined in resource) from the each subclass (a bitmap that is global (static)) Thank's in...
6
by: David Rasmussen | last post by:
If I have a collection of dicts like: john = {'id': 1, 'name': "John Cleese", 'year': 1939} graham = {'id': 2, 'name': "Graham Chapman", 'year': 1941} I could store all of them in a list. But...
44
by: gregory.petrosyan | last post by:
Hello everybody! I have little problem: class A: def __init__(self, n): self.data = n def f(self, x = ????) print x All I want is to make self.data the default argument for self.f(). (I
15
by: George Sakkis | last post by:
Although I consider dict(**kwds) as one of the few unfortunate design choices in python since it prevents the future addition of useful keyword arguments (e.g a default value or an orderby...
22
by: jeremito | last post by:
I am writing a class that is intended to be subclassed. What is the proper way to indicate that a sub class must override a method? Thanks, Jeremy
12
by: jeremito | last post by:
Please excuse me if this is obvious to others, but I can't figure it out. I am subclassing dict, but want to prevent direct changing of some key/value pairs. For this I thought I should override...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.