Python vs. Perl

Alex Martelli aleaxit at yahoo.com
Sat May 26 02:50:53 EDT 2001


"Gisle Aas" <gisle at ActiveState.com> wrote in message
news:m366eolpy8.fsf at ActiveState.com...
> "Alex Martelli" <aleaxit at yahoo.com> writes:
>
> > > Perl: %hash = (a=>{a=>'b', b=>'c', c=>'a'}, b=>{...etc...}); This is
> > pretty
> >     ...
> > > Python excels at dictionaries. Frankly, I find python's syntax a bit
> > > burdensome and wordy.
> >
> > hash = {a:{a:'b', b:'c', c:'a'}, b:{et:c}}
> >
> > How is this "burdensome and wordy" compared to the Perl syntax
> > you just quoted so admiringly...?!
>
> You would actually have to write {'a':{'a': ... in Python
> to have the same effect as the Perl code above.  That clearly
> makes the perl key syntax more concise.

Ah, the keys in a dictionary (hash) display are taken as literal
and the values aren't?  How quaint.  I used to know Perl pretty
well and wrote tens thousands of lines in it -- I'm so glad I am
at last forgetting its gyrations!-)  Is it a stropping issue?  Why
are the quotes needed after the '=>'s -- or, ARE they?

If I did have to display-construct a mapping whose keys are all
strings and needed concision I might use a function (rather than
class) variant of my cookbook recipe:
http://aspn.activestate.com/ASPN/Python/Cookbook/Recipe/52308

    def D(**kw): return kw
    mp=D(a=D(a='b',b='c',c='a'), b=D(et='c'))

as in the named-argument syntax x=y, x IS taken as an
identifier rather than an expression.  But this only works
for keys that are _identifier_-like strings -- can't use it
for a key that is, say, '=>'.  I assume
    (=>=>'=>')
would be a 'problem' in Perl, too?  Or not?-)

> but then unfortunately Perl looses big time when you don't want
> use variables as keys:
>
>         $a=>
> vs      a:

Is it "don't want" or 'do want'?  It's more general than variables:
in a dictionary-display in Python, I can use any expression on either
side of the ':'.  If this holds in Perl, too, then the difference seems
to be a general issue of stropping -- in Python I can use a bareword
identifier as a variable name in all contexts (if on the left of '=', it
is a variable being bound rather than referenced -- e.g., the above
mentioned named-argument syntax is binding the variables that
are arguments to the function) -- in Perl I have to strop a variable
name ($a, %a, @a) but in a few contexts I can use the bareword
without quotes (not in most contexts, right?  used to be OK in Perl
3, as I recall, but it's been at-least-deprecated for a long time?).


Alex








More information about the Python-list mailing list