Create child class of Python dict with modified values

D

dmitrey

hi all,
suppose I have defined a child class of Python dict, currently it
constructor looks like that:
def __init__(self, *args, **kwargs):
dict.__init__(self, *args, **kwargs)
#(+some more insufficient code)

Constructor should be capable of calling with either any way Python
dict is constructed or with a Python dict instance to be derived from;
calculations speed is important.

So it works well for now, but I want __init__ to set modified values,
like this:
values_of_the_dict = [some_func(elem) for elem in self.values()]

How this could be done?

Thank you in advance,
Dmitrey.
 
P

Peter Otten

dmitrey said:
hi all,
suppose I have defined a child class of Python dict, currently it
constructor looks like that:
def __init__(self, *args, **kwargs):
dict.__init__(self, *args, **kwargs)
#(+some more insufficient code)

Constructor should be capable of calling with either any way Python
dict is constructed or with a Python dict instance to be derived from;
calculations speed is important.

So it works well for now, but I want __init__ to set modified values,
like this:
values_of_the_dict = [some_func(elem) for elem in self.values()]

How this could be done?
.... def __init__(self, *args, **kw):
.... if args:
.... args = ((k, v.upper()) for k, v in args[0]),
.... if kw:
.... for k in kw: kw[k] = 10*kw[k]
.... dict.__init__(self, *args, **kw)
....
{'a': 'B', 'c': 'D', 'e': 'ffffffffff'}

Replace v.upper() and 10*kw[k] with the appropriate some_func() calls.
Personally I would apply the function before passing the data to the dict
subclass.

Peter
 
D

dmitrey

dmitrey said:
hi all,
suppose I have defined a child class of Python dict, currently it
constructor looks like that:
    def __init__(self, *args, **kwargs):
        dict.__init__(self, *args, **kwargs)
        #(+some more insufficient code)
Constructor should be capable of calling with either any way Python
dict is constructed or with a Python dict instance to be derived from;
calculations speed is important.
So it works well for now, but I want __init__ to set modified values,
like this:
values_of_the_dict = [some_func(elem) for elem in self.values()]
How this could be done?

...     def __init__(self, *args, **kw):
...             if args:
...                     args = ((k, v.upper()) for k, v in args[0]),
...             if kw:
...                     for k in kw: kw[k] = 10*kw[k]
...             dict.__init__(self, *args, **kw)
...>>> D(["ab", "cd"], e="f")

{'a': 'B', 'c': 'D', 'e': 'ffffffffff'}

Replace v.upper() and 10*kw[k] with the appropriate some_func() calls.

OK, thank you.
Personally I would apply the function before passing the data to the dict
subclass.

It's impossible for the situation in hand.

D.
 

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

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top