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

Max M maxm at mxm.dk
Mon Feb 18 06:59:45 EST 2002


Hi

It's just a little thing that has been bugging me for a while.

I came to Python from JScript and was used to every object being both an 
object and a dictionary.

So If I want to get an the value form an objects attribute I would just 
write:

myObj = new Object()
myObj.name = 'max';
myObj.age = 36;

print myObj.name; // pretending that JScript has a print function :-)
 >>> max
print myObj.name;
 >>> 36

or

print myObj['name']; // pretending that JScript has a print function :-)
 >>> max
print myObj['name'];
 >>> 36

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']


I don't really understand why the setattr/getattr should be nessecary 
when it can be done much simpler with the dictionary notation.

ie. in Zope an object is written anyway that takes both notations, and 
really it should not be nessecary when it could just as easily be built 
into the objects.

Wouldn't it be a good idea to unify the dict and the object so that both 
could be called with attribute and dict notation like in JScript?

I have not really tried 2.2 yet so I am not aware if doing something like:

class myClass(dict, object):
     pass

will give the functionality I am after.

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

I have not really tried 2.2 yet so I am not aware if doing something like:

class myClass(dict, object):
     pass

will give the functionality I am after.

regards Max M




More information about the Python-list mailing list