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

Jason Orendorff jason at jorendorff.com
Mon Feb 18 14:34:02 EST 2002


> 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 
> it in Python where we have several notations for the same action:
> 
> In an object:
> setattr(myObj, 'name', 'max')
> getattr(myObj, 'name')
> 
> In a dict:
> myObj['name'] = 'max'
> myObj['name']

They're not really the same action in Python.

Dictionaries have methods in Python. For example:

  d = {'keys': 'a b c', 'values': 'x y z'}
  print d.keys()

Here "d.keys" is a method.  It does not refer to the string
'a b c'.  Should it?

One way around this is to say that data structures shouldn't have
methods at all.  This is the approach JavaScript takes - and there
is nothing at all wrong with it; it's just a different way of
seeing things.

I like using methods to operate on data structures.
It's convenient.  So I'm fond of Python's approach.


> I know it's just syntactic sugar, but it would be so sweet.
> Is there any reason why not to do it?

Just a design difference between JavaScript and Python, really.
In Python, attributes (and methods) are quite different in meaning
from dictionary keys.

Good question.

## Jason Orendorff    http://www.jorendorff.com/




More information about the Python-list mailing list