Dictionary from list?

Tim Hammerquist tim at vegeta.ath.cx
Fri Oct 19 18:16:54 EDT 2001


Me parece que Erik Max Francis <max at alcyone.com> dijo:
[ snip ]
>  In particular, what this is really revealing is that a Perl associative
>  array is really represented as a list, with each successive pair
>  representing a key and a value.

That's incorrect, or at least deceptively vague. Perl hashes may be
_assigned_ a list which is parsed and initialized using the algorithm
you described.  This can easily be implemented in Python by simply
subclassing and providing a constructor to this effect.

When passed unadorned to the print function, they are also interpolated
as lists are.  This, also, can be implemented using the __repr__ method
in the subclass mentioned above.

Internally, however, Perl hashes and Python dictionaries are treated
very similarly.  It is merely their interface that differs. Is this not
the advantage of OOP?

I'm not by any means recommending subclasses UserDict to create a
PerlDict or some such.  It's merely to demonstrate how simple it is to
change Python's dicts to behave as Perl hashes, and vice versa.

>  This goes back to Perl's weakly-typed approach to everything.  Since
>  Python is not weakly typed, emulating this behavior is not only not
>  convenient, but is not even advisable.

This has nothing to do weak-typing, strong-typing, or touch-typing.  It
has to do with a language's data-type interface.

Were you perhaps thinking of Tcl?

Tim
-- 
The biggest problem with communication is the illusion that it has occurred.



More information about the Python-list mailing list