"literal" objects

Bengt Richter bokr at oz.net
Wed Dec 24 17:26:50 EST 2003


On Wed, 24 Dec 2003 09:23:23 -0800, Donn Cave <donn at u.washington.edu> wrote:

>In article <%QbGb.2109$1f6.732 at newssvr25.news.prodigy.com>,
> "Moosebumps" <purely at unadulterated.nonsense> wrote:
[...]
>
>> A thought that occured to me is that classes are implemented as dictionaries
>> (correct?).  So you could have a dictionary like this:
>> 
>> x = {'a': 3, 'b': 5}
>> y = {'a': 5, 'b': 15}
>> 
>> This would be the __dict__ attribute of an object I suppose.  But I don't
>> see anyway to assign it to a variable so you could access them like x.a and
>> y.a.  I don't know if this would be a "nice" way to do it or not.
>
>class A:
>    def __init__(self):
>        self.__dict__.update({'a': 3, 'b': 5})
>x = A()
>print x.a, x.b
>
>Would it be nice?  No, it would be horrible, unless you had some
>very compelling reason to do this, in which case it would be fine.
This form of the above can be useful, though:

 class A:
     def __init__(self, **kw):
         self.__dict__.update(kw)
 x = A(a=3, b=5)
 print x.a, x.b

Since you can then also continue with

 y = A(a='Happy', b='Holidays')
 print y.a, y.b

;-)

Regards,
Bengt Richter




More information about the Python-list mailing list