a clean way to define dictionary
Jeremy Hylton
jeremy at alum.mit.edu
Wed Jun 18 17:06:58 EDT 2003
On Wed, 2003-06-18 at 04:22, Alex Martelli wrote:
> > also, i don't think it's a good idea to use dict as a variable name since
> > dict is a python type.
>
> Very good advice! If you use dict as a local name, you shadow the
> buit-in name 'dict' and therefore cannot any more call it e.g. in
> order to generate a new dictionary, as above.
Agreed. When things go wrong, it can be very difficult to understand.
Say you use file as a local variable, but through later editing delete
the assignment. Later, you call file.read(25) or dict.update(obj). You
get:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: descriptor 'read' requires a 'file' object but received a
'int'
It's not obvious that you've called the unbound method on the builtin
type.
Jeremy
More information about the Python-list
mailing list