There is more than one way to do it - and for no apparent reason.

Max M maxm at mxm.dk
Mon Feb 18 08:36:11 EST 2002


Alex Martelli wrote:
> Max M wrote:
>         ...
> 
>>Basically you can say that JScript uses the dictionary notation for
>>getting attributes. As fas I can see this is smarter than the way we do
>>
> Maybe you can't see very far....?-)

That's probably true (-6,-6.5)  on left and right eye ;-)

> adict = dict()
> somerandomwordfromsomewhere = 'get'
> adict[somerandomwordfromsomewhere] = 23
> 
> Now what should adict.get be?  The number 23, or a bound method that you
> can save somewhere and later call with an argument or two?

Well it should be 23 because you have assigned it to that.

> If items and attributes were confused, you basically couldn't use
> bound methods any more -- you could never be sure if by some
> mischance somebody has set an item called get, or keys, or items,
> or setdefault, or ... whatever other name you might like to use.

Yes I see your point here. The reason that it can be used in JScript is 
because you allways itterate it by key like::

     for (var key in myObj){
         val = myObj[i];
         print(key, val);
     }

And if we did the same in Python we would risk overwriting some of the 
bound methods by accident::

     dirtyList = ['keys', 'values', 'items']

     newDict = {}
     for item in dirtyList:
         newDict[item] = 42 # this would indeed make the dict hard to 
use :-)

So I agree that unifying object and dict would be a bad idea.

But we could still use the dict like notation on an objects without any 
greater risk than that imposed by getattr/setattr. And that was in fact 
my main idea behind the question.

well I can allways just mix-in my own class

class IWILLHaveItMyWay:

     def __getitem__(self, key):
         return getattr(self, key)

     def __setitem__(self, key, value):
         setattr(self, key, value)

regards Max M




More information about the Python-list mailing list