Alternate Syntax for dictionary elements

Gerson Kurz gerson.kurz at t-online.de
Tue Jul 3 15:56:59 EDT 2001


On 3 Jul 2001 19:25:31 GMT, quinn at hork.ugcs.caltech.edu (Quinn Dunkan)
wrote:

>And here's why using a dict as you did above is probably silly: You don't need
>to manually encode type information if the language can do it for you.  Making
>a Button class will automatically attach that type to its instances.
>Furthermore, a fundamental property of all objects (as opposed to true values
>like an algebraic data type) is that they have unique ids seperate from their
>values.  The language will maintain them for you, and to fetch them you can
>use the id() function. The 'name' field is the only necessary one.
>
>"Yes", you're saying, "but it was only an example." :)

Well, you're right, I wrote up classes later, but mainly so that the
code is more easily extended. 

2 reasons for using dicts rather than classes:

- You can easily create the data an object stores *dynamically*, i.e.
from user input.

- The class approach is a lot of overhead compared with writing a
simple dict, when all you want to store is a name. 

Plus, classes are slower :

Xoring a variable in a dict 1000000 times took 2.91 seconds.
Xoring a variable in a class 1000000 times took 3.39 seconds.




More information about the Python-list mailing list