dict.updated

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rick Morrison

    #1

    dict.updated

    Would there be any way to add a method to all dict objects that operated
    like the .update() method, but also returned a reference to the updated
    dict?

    ..update() is clumsy to use inside of list comprehensions and the like. Or am
    I missing something?

    Thanks,
    Rick




  • Steven Bethard

    #2
    Re: dict.updated

    Rick Morrison wrote:[color=blue]
    > Would there be any way to add a method to all dict objects that operated
    > like the .update() method, but also returned a reference to the updated
    > dict?[/color]

    Are you looking for updated() to parallel sorted(), where sorted()
    returns a *new* list? I doubt you'll be able to rally much support for
    a method that changes an object and returns a reference to it -- check
    the archives to see these kind of things getting rejected over and over.

    Could you do something like:

    py> import itertools
    py> d1 = dict(a=1, b=2)
    py> d2 = dict(c=3, d=4)
    py> d3 = dict(itertools. chain(d1.iterit ems(), d2.iteritems()) )
    py> d3
    {'a': 1, 'c': 3, 'b': 2, 'd': 4}

    Steve

    Comment

    • Rick Morrison

      #3
      Re: dict.updated

      I could live with creating a new dict, sure (although it seems wasteful). I
      realize that something like this probably doesn't stand a chance of ever
      making it into the std library for what might be called "philosophi cal"
      reasons. I just want it for me (my personal philosophy runs more to the
      pragmatic -- well at least for coding).

      I suppose the in-place version would be more along the lines of:
      [color=blue][color=green][color=darkred]
      >>> def updated(d, updates):[/color][/color][/color]
      .... d.update(update s)
      .... return d
      ....[color=blue][color=green][color=darkred]
      >>> [updated(d, {'c':3}) for d in [{'a':1, 'b':2}, {'x':10, 'y':'11'}]][/color][/color][/color]
      [{'a': 1, 'c': 3, 'b': 2}, {'y': '11', 'x': 10, 'c': 3}]

      But I'd like to put the "updated" method on the "dict" object, which is what
      I can't seem to figure out.
      Yeah I know that's "bad", but to my mind so is polluting the global
      namespace with the "updated" function.

      -- Or maybe I'm just lazy and picked up too many bad habits from
      Javascript.

      Rick


      "Steven Bethard" <steven.bethard @gmail.com> wrote in message
      news:dvKdnWal2s VXAXjcRVn-2g@comcast.com. ..[color=blue]
      > Rick Morrison wrote:[color=green]
      > > Would there be any way to add a method to all dict objects that operated
      > > like the .update() method, but also returned a reference to the updated
      > > dict?[/color]
      >
      > Are you looking for updated() to parallel sorted(), where sorted()
      > returns a *new* list? I doubt you'll be able to rally much support for
      > a method that changes an object and returns a reference to it -- check
      > the archives to see these kind of things getting rejected over and over.
      >
      > Could you do something like:
      >
      > py> import itertools
      > py> d1 = dict(a=1, b=2)
      > py> d2 = dict(c=3, d=4)
      > py> d3 = dict(itertools. chain(d1.iterit ems(), d2.iteritems()) )
      > py> d3
      > {'a': 1, 'c': 3, 'b': 2, 'd': 4}
      >
      > Steve[/color]


      Comment

      • Stephen Thorne

        #4
        Re: dict.updated

        On Wed, 12 Jan 2005 23:47:13 GMT, Rick Morrison
        <ram@forefron t-tech.no.lunch.m eat.com> wrote:[color=blue]
        > I could live with creating a new dict, sure (although it seems wasteful). I
        > realize that something like this probably doesn't stand a chance of ever
        > making it into the std library for what might be called "philosophi cal"
        > reasons. I just want it for me (my personal philosophy runs more to the
        > pragmatic -- well at least for coding).
        >
        > I suppose the in-place version would be more along the lines of:
        >[color=green][color=darkred]
        > >>> def updated(d, updates):[/color][/color]
        > ... d.update(update s)
        > ... return d
        > ...[color=green][color=darkred]
        > >>> [updated(d, {'c':3}) for d in [{'a':1, 'b':2}, {'x':10, 'y':'11'}]][/color][/color]
        > [{'a': 1, 'c': 3, 'b': 2}, {'y': '11', 'x': 10, 'c': 3}]
        >
        > But I'd like to put the "updated" method on the "dict" object, which is what
        > I can't seem to figure out.
        > Yeah I know that's "bad", but to my mind so is polluting the global
        > namespace with the "updated" function.[/color]

        [dict(d.items() + {'c':3}.items() ) for d in [{'a':1, 'b':2}, {'x':10,
        'y':'11'}]]
        seems logical enough to me....
        [color=blue]
        > -- Or maybe I'm just lazy and picked up too many bad habits from
        > Javascript.[/color]

        probably.

        Stephen.

        Comment

        • Steven Bethard

          #5
          Re: dict.updated

          Rick Morrison wrote:[color=blue]
          > I could live with creating a new dict, sure (although it seems wasteful). I
          > realize that something like this probably doesn't stand a chance of ever
          > making it into the std library for what might be called "philosophi cal"
          > reasons. I just want it for me (my personal philosophy runs more to the
          > pragmatic -- well at least for coding).
          >
          > I suppose the in-place version would be more along the lines of:
          >
          >[color=green][color=darkred]
          >>>>def updated(d, updates):[/color][/color]
          >
          > ... d.update(update s)
          > ... return d
          > ...
          >[color=green][color=darkred]
          >>>>[updated(d, {'c':3}) for d in [{'a':1, 'b':2}, {'x':10, 'y':'11'}]][/color][/color]
          >
          > [{'a': 1, 'c': 3, 'b': 2}, {'y': '11', 'x': 10, 'c': 3}]
          >
          > But I'd like to put the "updated" method on the "dict" object, which is what
          > I can't seem to figure out.
          > Yeah I know that's "bad", but to my mind so is polluting the global
          > namespace with the "updated" function.[/color]

          You could do something like:

          py> class dict(dict):
          .... def updated(self, *args, **kwds):
          .... self.update(*ar gs, **kwds)
          .... return self
          ....
          py> [d.updated(c=3) for d in [dict(a=1, b=2), dict(x=10, y=11)]]
          [{'a': 1, 'c': 3, 'b': 2}, {'y': 11, 'x': 10, 'c': 3}]

          It'd mean you'd have to create all your dicts with the dict constructor
          instead of {} though.

          Steve

          Comment

          • hanz

            #6
            Re: dict.updated


            Rick Morrison wrote:[color=blue][color=green][color=darkred]
            > >>> [updated(d, {'c':3}) for d in [{'a':1, 'b':2}, {'x':10,[/color][/color][/color]
            'y':'11'}]][color=blue]
            > [{'a': 1, 'c': 3, 'b': 2}, {'y': '11', 'x': 10, 'c': 3}][/color]

            I don't really understand the use of this. Can you give a less toy
            example? I'd probably just do

            dicts = [{'a':1, 'b':2}, {'x':10, 'y':'11'}]
            for d in dicts: d.update({'c':3 })

            This isn't really more typing or any clumsier IMO, so I can't see why
            you'd want to use list comprehensions in this case.

            Comment

            • Bengt Richter

              #7
              Re: dict.updated

              On Wed, 12 Jan 2005 23:47:13 GMT, "Rick Morrison" <ram@forefron t-tech.No.Lunch.m eat.com> wrote:
              [color=blue]
              >I could live with creating a new dict, sure (although it seems wasteful). I
              >realize that something like this probably doesn't stand a chance of ever
              >making it into the std library for what might be called "philosophi cal"
              >reasons. I just want it for me (my personal philosophy runs more to the
              >pragmatic -- well at least for coding).
              >
              >I suppose the in-place version would be more along the lines of:
              >[color=green][color=darkred]
              >>>> def updated(d, updates):[/color][/color]
              >... d.update(update s)
              >... return d
              >...[color=green][color=darkred]
              >>>> [updated(d, {'c':3}) for d in [{'a':1, 'b':2}, {'x':10, 'y':'11'}]][/color][/color]
              >[{'a': 1, 'c': 3, 'b': 2}, {'y': '11', 'x': 10, 'c': 3}]
              >[/color]
              It's kind of a strange list comp, but I guess you're just illustrating something.
              But for this case you could just write
              [color=blue][color=green][color=darkred]
              >>> [d.update({'c':3 }) or d for d in [{'a':1, 'b':2}, {'x':10, 'y':'11'}]][/color][/color][/color]
              [{'a': 1, 'c': 3, 'b': 2}, {'y': '11', 'x': 10, 'c': 3}]

              taking advantage of normal d.update() returning None and so forcing the 'or' term.


              Regards,
              Bengt Richter

              Comment

              Working...