RE: Dictionary assignment

Discussion in 'Python' started by Mark Daley, Aug 15, 2003.

  1. Mark Daley

    Mark Daley Guest

    I'm becoming aware that my coding techniques leave much to be desired.
    That's ok, because I'm really just a hack with no education whatsoever. At
    any rate, I realize I didn't supply enough information. Here is the core of
    my dilemma:

    format[self.formats.get()][key] = current[key]

    Now, all disgust from my example aside, shouldn't this line cause a new key
    (whatever self.formats get() produces) whose contents are an exact copy of
    current?

    BTW, I do this to avoid those location references. I need to get an actual
    copy and this is the only way I can (currently) guarantee it. Believe me,
    I'm only treading water here!

    - Mark

    -----Original Message-----
    From:
    [mailto:p]On Behalf Of John J. Lee
    Sent: Friday, August 15, 2003 3:25 PM
    To:
    Subject: Re: Dictionary assignment


    "Mark Daley" <> writes:

    > I've been using this to save one dictionary as an entry in another
    > dictionary. I was working, but now it seems I've done something to break
    > it. Here's the code in question:
    >
    > def formatsave(self, args = None):
    > if self.formats.get() == '':
    > tkMessageBox.showwarning("No Format", "You must specify a

    format
    > name.")
    > else:
    > for key in current.keys():
    > format[self.formats.get()][key] = current[key]
    > temp = format.keys()
    > temp.sort()
    > list = tuple(temp)


    Yuck. You've assigned something to a builtin (list). Strangely, the
    object you chose to bind to list is a tuple!


    > gui.formats._list.setlist(list)
    >
    >
    > Here's the error I'm getting:
    >
    > Traceback (most recent call last):
    > File "C:\PYTHON23\lib\lib-tk\Tkinter.py", line 1345, in __call__
    > return self.func(*args)
    > File "C:\Python23\Layout.py", line 191, in formatsave
    > format[self.formats.get()][key] = current[key]
    > KeyError: 'Format 1'
    >
    >
    > Any ideas?


    Nobody here is likely to solve it for you. Get aquainted with the
    print statement! There are three indexes there -- which one is the
    exception coming from? Split them up, one index per line, and print
    out the results. You'll soon see the problem.

    print format[self.formats.get()]
    print format[self.formats.get()][key]
    print current[key]

    If you're like me, you'll want to make sure all debug print statements
    have a label, though, or you'll inevitably end up wondering (not now,
    but later) where the hell the output you're seeing on the screen is
    coming from.

    print "format[self.formats.get()]", format[self.formats.get()]
    print "format[self.formats.get()][key]", format[self.formats.get()][key]
    print "current[key]", current[key]


    John
    --
    http://mail.python.org/mailman/listinfo/python-list
     
    Mark Daley, Aug 15, 2003
    #1
    1. Advertising

  2. Mark Daley

    Terry Reedy Guest

    "Mark Daley" <> wrote in message
    news:...
    > format[self.formats.get()][key] = current[key]


    Above statement assumes 1) that current is a sequence or dict (or
    instance simulating such) and that the object associated with key is
    an index or key therein. It retrieves the associated object. It then
    assumes 2) that format is a sequence or dict (according to whether
    self.formats.get() returns an int index or dict key) of mutable
    sequences or dicts (according to the nature of key). After retrieving
    the appropriate collection, it associates key with the
    retrieved-from-current object in that collection.

    > Now, all disgust from my example aside, shouldn't this line cause a

    new key
    > (whatever self.formats get() produces) whose contents are an exact

    copy of
    > current?


    The only new 'thing' created is the association between key and
    current[key] in format[self.formats.get()]. There are no new Python
    objects created.

    > BTW, I do this to avoid those location references.


    No idea of what this means.

    > I need to get an actual copy


    Of what?

    > and this is the only way I can (currently) guarantee it.


    Nope. Assignment 'creates' bindings (of object to target), not
    objects or copies thereof.

    Terry J. Reedy
     
    Terry Reedy, Aug 16, 2003
    #2
    1. Advertising

  3. Mark Daley wrote:

    > format[self.formats.get()][key] = current[key]
    >
    > Now, all disgust from my example aside, shouldn't this line cause a new key
    > (whatever self.formats get() produces) whose contents are an exact copy of
    > current?


    First you need to assure that format[self.formats.get()] contains
    another dictionary as a value. Here is an example:

    >>>> a = {}
    >>>> a[1] = {}
    >>>> a[1][2] = 1


    works as expected but

    >>>> a[2][3] = 4


    will fail with KeyError:2 because this construct attempts to fetch the
    value corresponding to key 2 and then add to that dictionary the 3/4
    key/value pair.

    It is indeed confusing, I never thought about it and caught me by
    surprise that it is KeyError:2 that is being thrown.

    As one parses the expression from left to right it reads as if the
    assignment to key 2 comes first and only then does the assignment
    to key 3 become "active". The error message one expects is that a[2]
    does not support assigment.

    Istvan.
     
    Istvan Albert, Aug 16, 2003
    #3
  4. Mark Daley

    John J. Lee Guest

    Istvan Albert <> writes:

    > Mark Daley wrote:
    >
    > > format[self.formats.get()][key] = current[key]
    > > Now, all disgust from my example aside, shouldn't this line cause a
    > > new key
    > > (whatever self.formats get() produces) whose contents are an exact copy of
    > > current?

    >
    > First you need to assure that format[self.formats.get()] contains
    > another dictionary as a value. Here is an example:
    >
    > >>>> a = {}
    > >>>> a[1] = {}
    > >>>> a[1][2] = 1

    >
    > works as expected but
    >
    > >>>> a[2][3] = 4

    >
    > will fail with KeyError:2 because this construct attempts to fetch the
    > value corresponding to key 2 and then add to that dictionary the 3/4
    > key/value pair.
    >
    > It is indeed confusing, I never thought about it and caught me by
    > surprise that it is KeyError:2 that is being thrown.


    Maybe it's surprising to some Perl users. Personally, it doesn't
    surprise me (and I did come from Perl) that if you didn't add a key 2
    to the dictionary a, it complains when you look up 2 in that dict.


    > As one parses the expression from left to right it reads as if the
    > assignment to key 2 comes first and only then does the assignment
    > to key 3 become "active".


    That's precisely what *does* happen, if you replace the first
    'assignment to' in that sentence with 'item lookup' or 'indexing'.
    The key 2 is not automagically created when it's found to be missing.
    Perhaps you wanted:

    a.setdefault(2, {})[3] = 4

    ?

    How would it know what to index-assign to a[2] otherwise? A dict? A
    list? Something else?


    > The error message one expects is that a[2]
    > does not support assigment.


    a[2] doesn't exist in your example above.


    John
     
    John J. Lee, Aug 16, 2003
    #4
  5. John J. Lee wrote:

    > Maybe it's surprising to some Perl users. Personally, it doesn't
    > surprise me (and I did come from Perl) that if you didn't add a key 2
    > to the dictionary a, it complains when you look up 2 in that dict.


    What I meant to say is that it looks confusing because the
    expression

    a[2][3] = 4

    looks like and straightforward assignment but resolves to a lookup
    followed by an assignment. That is the reason the original
    poster had the problem in question.

    Istvan.
     
    Istvan Albert, Aug 16, 2003
    #5
    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. Ilias Lazaridis
    Replies:
    6
    Views:
    454
    Ilias Lazaridis
    Feb 21, 2006
  2. james_027
    Replies:
    1
    Views:
    338
    Marc 'BlackJack' Rintsch
    Aug 22, 2007
  3. Navkirat Singh
    Replies:
    6
    Views:
    3,164
    Navkirat Singh
    Jul 29, 2010
  4. Chris Rebert
    Replies:
    0
    Views:
    541
    Chris Rebert
    Jul 29, 2010
  5. Fox

    dictionary within dictionary

    Fox, Mar 8, 2005, in forum: ASP General
    Replies:
    5
    Views:
    199
    Michael D. Kersey
    Mar 13, 2005
Loading...

Share This Page