Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > RE: Dictionary assignment

Reply
Thread Tools

RE: Dictionary assignment

 
 
Mark Daley
Guest
Posts: n/a
 
      08-15-2003
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: http://www.velocityreviews.com/forums/(E-Mail Removed)
[mailto(E-Mail Removed)]On Behalf Of John J. Lee
Sent: Friday, August 15, 2003 3:25 PM
To: (E-Mail Removed)
Subject: Re: Dictionary assignment


"Mark Daley" <(E-Mail Removed)> 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



 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      08-15-2003

"Mark Daley" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
Istvan Albert
Guest
Posts: n/a
 
      08-16-2003
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.


 
Reply With Quote
 
John J. Lee
Guest
Posts: n/a
 
      08-16-2003
Istvan Albert <(E-Mail Removed)> 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
 
Reply With Quote
 
Istvan Albert
Guest
Posts: n/a
 
      08-16-2003
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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Performance ordered dictionary vs normal dictionary Navkirat Singh Python 6 07-29-2010 10:18 AM
Re: Performance ordered dictionary vs normal dictionary Chris Rebert Python 0 07-29-2010 06:11 AM
creating a dictionary from a dictionary with regex james_027 Python 1 08-22-2007 07:39 AM
[DICTIONARY] - Copy dictionary entries to attributes Ilias Lazaridis Python 6 02-21-2006 11:27 AM
dictionary within dictionary Fox ASP General 5 03-13-2005 05:03 AM



Advertisments