Python vs. Perl

Jonathan Gardner gardner at cardomain.com
Fri May 25 13:25:42 EDT 2001


On Thursday 24 May 2001 07:12 pm, Paul Prescod wrote:
> Jonathan Gardner wrote:
> >
> > Also, I don't think anyone can claim that Python's dicts are better than
> > Perl's hashes or C++ STL's map. Well, maybe Perl's hashes are a little
> > better because you can do so much stuff with it in so many ways...
>
> I make that claim. It is trivial in Python to make dictionaries of
> dictionaries. It is harder in the other languages. It is also possible
> to use almost any object as the key. You can also have hashes of pairs,
> triples, or n-tuples of objects. Also, any object can choose how to
> represent itself in terms of a hash.

I must be missing the point. Please explain how Python is better than Perl in 
this respect.

"Dictionaries of Dictionaries"

C/C++: I'll admit that in C++ STL some interpreters have problems with 
templates that have template types that are templates themselves. For 
instance, map<int, map<int, string>> fails in at least the Microsoft 
compiler, but you can use typedefs to get around that. It is wordy, and not 
very friendly, but the syntax is clear and easy to follow.

Besides, you *could* use void pointers and typecast them as necessary, 
although this is messy. 

Conclusion: C++ is messy, but not impossible.

Perl: %hash = (a=>{a=>'b', b=>'c', c=>'a'}, b=>{...etc...}); This is pretty 
straightforward. In addition to that, if one of the scalars is not declared 
yet and you treat it like it should be a reference to a hash, it becomes a 
reference to a hash. This is a very useful feature - you can forget about 
initialization, safely 100% of the time. Just don't start talking about a 
hash and then think you were talking about an array.

Conclusion: Either Perl is better than Python, or Python and Perl have the 
same featureset. I can't imagine anything that can be better than this. 
Please mention it if you know.

"Dictionaries of tuples"

Perl has as straightforward and easy syntax for making arrays or hashes, 
hashes of arrays, and arrays of arrays or hashes of hashes. The references 
idea may be a bit confusing AT FIRST, but in the end, it is extremely 
powerful and useful - just like C pointers are confusing but end up being 
extremely useful for many things.

"Using Objects as Keys"

Okay, you got me there. I got burned big time in perl because the keys 
weren't real scalars - they don't have any magic. I do want to use objects as 
keys in hashes and this cannot be done in Perl.

This is one of those things that should be used in the argument against Perl.

"Objects representing themselves as a Dictionary"

This is kind of cool. In the OO arena, Python wins hands down over C++ and 
Perl.

I'm not trying to rile anyone up, I just want to have someone show me how 
Python excels at dictionaries. Frankly, I find python's syntax a bit 
burdensome and wordy.




More information about the Python-list mailing list