Re: "literal" objects
From: Bengt Richter (bokr_at_oz.net)
Date: 12/24/03
- Next message: Benjamin Han: "Is re thread-safe?"
- Previous message: Dmitry Rozmanov: "Re: nonlinear least square"
- In reply to: Donn Cave: "Re: "literal" objects"
- Next in thread: Bjorn Pettersen: "Re: "literal" objects"
- Reply: Bjorn Pettersen: "Re: "literal" objects"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 24 Dec 2003 22:26:50 GMT
On Wed, 24 Dec 2003 09:23:23 -0800, Donn Cave <donn@u.washington.edu> wrote:
>In article <%QbGb.2109$1f6.732@newssvr25.news.prodigy.com>,
> "Moosebumps" <purely@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
- Next message: Benjamin Han: "Is re thread-safe?"
- Previous message: Dmitry Rozmanov: "Re: nonlinear least square"
- In reply to: Donn Cave: "Re: "literal" objects"
- Next in thread: Bjorn Pettersen: "Re: "literal" objects"
- Reply: Bjorn Pettersen: "Re: "literal" objects"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|