[portland] Seeking opinions on choosing data types: dictionary or instance attributes?
adam at therobots.org
Tue Nov 27 22:44:53 CET 2007
On Nov 27, 2007, at 12:23 PM, ray at bme.ogi.edu wrote:
> and I'm wondering when we should use instance attributes vs.
> when we should use dictionaries. (We'll have some lists when sequence
> matters) My friend loves dictionaries and says to make every class
> a dictionary; then instead of any instance attributes, have key
> value pairs.
First off, I'd suggest not making the class itself a dict (that is,
inheriting from UserDict). In my experience, unless you're really just
making a dictionary variant, not a class that uses a dictionary, it'll
end being more complicated. Others might disagree, though.
One common way is to have classes that contain a dictionary with the
self.values = <some default values or empty dict>
then internally you can use the values dict, but you won't have to
worry about accidentally changing the behavior of the dict or if
another non-dictionary storage starts to look better.
But to the original question; I would suggest writing a bit of
experimental code using the classes you're working on; that way you
can see which code is more readable.
More information about the Portland