Create child class of Python dict with modified values

Discussion in 'Python' started by dmitrey, Aug 9, 2010.

  1. dmitrey

    dmitrey Guest

    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.
    dmitrey, Aug 9, 2010
    #1
    1. Advertising

  2. dmitrey

    Peter Otten Guest

    dmitrey wrote:

    > 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?


    >>> class D(dict):

    .... 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.
    Personally I would apply the function before passing the data to the dict
    subclass.

    Peter
    Peter Otten, Aug 9, 2010
    #2
    1. Advertising

  3. dmitrey

    dmitrey Guest

    On Aug 9, 1:38 pm, Peter Otten <> wrote:
    > dmitrey wrote:
    > > 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?
    > >>> class D(dict):

    >
    > ...     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.
    dmitrey, Aug 9, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Rodriguez
    Replies:
    23
    Views:
    1,083
    David Schwartz
    Dec 9, 2003
  2. Replies:
    11
    Views:
    440
    Mike Meyer
    Jan 12, 2006
  3. Menghan Zheng
    Replies:
    1
    Views:
    260
    alex23
    Apr 20, 2010
  4. Cameron Simpson
    Replies:
    6
    Views:
    339
    alex23
    Apr 21, 2010
  5. macm
    Replies:
    11
    Views:
    590
    Alexander Gattin
    Nov 11, 2010
Loading...

Share This Page