[portland] Seeking opinions on choosing data types: dictionary or instance attributes?

ray at bme.ogi.edu ray at bme.ogi.edu
Tue Nov 27 23:43:41 CET 2007


> Use classes, that is what they are for. It should be mentioned that
> dicts are cool, which is why all namespaces in Python are implemented
> as dicts. That includes class attributes.

Sorry, I wasn't clear on that.  He wants classes, but he wants all the  
classes to inherit from dictionary.

This example is very helpful.  Thanks, Matt!

>>>> class C(object):
> ...  def __init__(self, a, b, c):
> ...   self.a = a
> ...   self.b = b
> ...   self.c = c
> ...
>>>> c = C(1,2,3)
>>>> d = C('a','b','c')
>>>> e = C(0.1, 0.2, 0.3)
>>>> f = C(c,d,e)
>>>> f.a.a
> 1
>>>> f.__dict__['a'].__dict__['a']
> 1
>>>> getattr(getattr(f,'a'),'a')
> 1
>
> Hopefully the above demonstrates how trivial it is to get an attribute
> when given the name of the attribute as a string.
>
> Matt
>




More information about the Portland mailing list