Help and optimization hints, anyone?
John J. Lee
jjl at pobox.com
Fri Jan 23 11:20:37 EST 2004
Kim Petersen <kim at lignus.dk> writes:
> Den Fri, 23 Jan 2004 12:32:04 +0000. skrev John J. Lee:
[...]
> > dict is also a bad name -- this is the name of the builtin dictionary
> > type, which you've just clobbered (in the local scope).
>
> Hmmm - isn't it called __dict__ ?
No, __dict__ is an attribute of Python objects. It's the dictionary
in which most Python objects keep their data:
>>> class foo:
... def __init__(self):
... self.blah = "stuff"
...
>>> f = foo()
>>> f.__dict__
{'blah': 'stuff'}
dict is the builtin name for the dictionary type:
>>> dict
<type 'dict'>
>>> dict({"foo": "bar", "spam": "eggs"})
{'foo': 'bar', 'spam': 'eggs'}
>>> dict([("foo", "bar"), ("spam", "eggs")])
{'foo': 'bar', 'spam': 'eggs'}
[...]
> > Haven't read much further, but it looks like you might be better off
> > using attribute access rather than indexing. In 2.2, use properties.
> > Earlier, use __setattr__ / __getattr__ (make sure you read the docs).
>
> this migrated from a regular dict which i had to drop because its
> not hashable as mentioned earlier - next step __(get|set)attr__.
dicts are deliberately not hashable, because they're mutable. Are you
sure you want a dict as a dictionary key (assuming that's what you're
doing)?
John
More information about the Python-list
mailing list