Weird errors when trying to access a dictionary key

Ben Finney bignose+hates-spam at benfinney.id.au
Fri Jul 20 19:18:54 EDT 2007


robinsiebler at gmail.com writes:

> I have a data structure that looks like this:
> [...]
> rpt_file.writelines('\t' + [song].keys() + '\t' + [song].values() + '\n')

Forms the list [song] twice, attempting to call the 'keys()' method
and the 'values()' method of that list in turn...

> I get the following error:
> 
> Traceback (most recent call last):
> [...]
> AttributeError: 'list' object has no attribute 'keys'

... which doesn't exist.

> Here is where it gets weird:
> 
> song
> {"I Don't Wanna Stop": 1}
> song.keys()
> ["I Don't Wanna Stop"]

Yes. A list doesn't have a 'keys' or 'values' method, a dict
does. 'song' is a dict. So why are you not calling the 'keys' method
of the 'song' object?

-- 
 \           "If [a technology company] has confidence in their future |
  `\      ability to innovate, the importance they place on protecting |
_o__)  their past innovations really should decline."  -- Gary Barnett |
Ben Finney



More information about the Python-list mailing list