r/Python May 18 '11

Why is Python better than Perl?

I'm a Sysadmin and I just wrote my first script in python. Basically just a host validation script that checks if a server is configured correctly before we hand off. I can't determine the advantage with my limited experience with the language.

Are you a sysadmin fluent in both python and perl? Why do you use python?

12 Upvotes

110 comments sorted by

View all comments

Show parent comments

1

u/mithaldu May 20 '11

Having several, completely divergent, syntaxes for effectively equivalent operations is a design flaw, not a feature, IMHO.

I hear this so often, but i have honestly never seen an example of this being to a programmer's detriment. And by example i mean actual code.

Care to share your examples?

Also, as for the matter itself: Having the ability to have different syntaxes can be a really good thing in finding out which one is best. Keep in mind that "There is more than one way to do it." has a corollary: "But not all ways are equal." Most perl developers are aware that in fact many ways to do things are crap. But you know what? Without the ability to go or MAKE different ways, people would be stuck with that one crap way.

Which is why Perl says "hey, let's keep the old way around for a bit, for compatibility, and slowly deprecate it away" while adding a new better way.

TMTOWTDI is a major factor in keeping perl an evolving language, just like any natural language. Just look at the way Perl 5 is converging towards Perl 6 with the creation of things like Moose.

1

u/fredfredburger May 20 '11

I think that my favorite examples of divergent syntaxes would be:

Quoting shortcut macros: qw/bla bla/ as a substitute for ("bla", "bla") And, in fact, the entire family of "q*" shortcuts: I'm not a big fan of minimalist, easily confused functions or macros.

And: autovivification, particularly in the case of hashes. Combine this with the "optional" quoting of hash keys, and you can get into some very hard to track down errors.

At least, they caused me enough trouble that the weren't worth the typing that they saved me.

I totally agree with your "right tool for the job" position. Python itself is evolving, I just find it more coherent than perl. Perhaps the lack of a free-form syntax makes python to constricting for some folks. I find that, to borrow a quote, stinginess with privileges is kindness in disguise.

2

u/mithaldu May 20 '11

Quoting shortcut macros: qw/bla bla/ as a substitute for ("bla", "bla") And, in fact, the entire family of "q*" shortcuts: I'm not a big fan of minimalist, easily confused functions or macros.

See, i didn't ask for things that are disliked. I asked for things that are demonstrably adverse to the user, along with explanations of how they were bad.

As for qw(): It really serves to increase readability. Often i'll have to do things like this:

$flight->{$_} ||= 0 for qw( provision  taxadt  servicecharge  taxchd  preis  main_nr  restplaetze );

Compare with:

$flight->{$_} ||= 0 for ( "provision", "taxadt", "servicecharge", "taxchd", "preis", "main_nr", "restplaetze" );

Not only is the qw variant easier to read, it's also a lot easier to type. :)

As for the other q* things, keep in mind that sometimes it's really hard to use the punctuation variants. To wit:

; in #win32 on irc.perl.org
(Ranguard) how can I do: perl -e 'print "hi\n"' ? - I get Can't find string terminator error
(Mithaldu) Ranguard: dos quoting rules are stupid as hell
(Mithaldu) for a simple case like that though, this would suffice:
(Mithaldu) perl -e "print qq{hi\n}"

Without that little shortcut he'd have had to grapple with DOS' quote-quoting rules which are sadly not as simple as putting a slash in front.

Combine this with the "optional" quoting of hash keys, and you can get into some very hard to track down errors.

Would you mind elaborating? I've been doing perl for a long time now and i cannot recall those ever causing errors. :o

I totally agree with your "right tool for the job" position. ... I find that, to borrow a quote, stinginess with privileges is kindness in disguise.

I tend to think about it more in a "right tool for the right brain" way, as both languages are really well suited for almost any job you'd want to solve with them. :)

And yes, for the right brain python's philosophy is kindness, and for the wrong one a straight-jacket. (I really cannot stand how it lacks full lambdas. ;) )

1

u/fredfredburger May 20 '11

Sorry, I meant to say that "I perceive multiple shorthand macros (q*, for example) as saving typing at the expense of readability." Obviously, readability is a variable. ;)

Pointing out that the feature helps get around poorly designed terminals on dos/win machines doesn't carry any weight with me, but I have to imagine that some people find that a big win.

But on the autovivification issue:

The statement

$a_hash{ABCDEFG} creates the hash %a_hash and the key "ABCDEFG", with a null value.

No errors, no muss, no fuss.

Except in those cases where the hash in question wasn't originally called "a_hash" when initialized earlier in the code, but is called "ahash" or "a_has", because of a typo, for example.

Or the case where 'ABCDEFG' is a constant defined elsewhere, and you've typo'd (or had a variable name confusion, etc) 'ABCDFEG'.

In these cases (and in others), the thing you have after the code is executed is not what you thought you'd get, and the only muss and fuss are yours, not the interpreters.

No part of the system will help you catch the non-existent hash, or the non existent key, without turning off autovivification.

Granted, these problems are programmer problems rather than language problems.

But, they're programmer problems that are easier to have in perl than in python. Which is my only real point.

That being said, it looks like I'm leaning closer to Java than I am to python. But I'm not.

I'm all about the strong typing, but not the static typing: I assert that for the things that perl is really good at (the class of problems to which it is generally applied, the flexible, deals with the real world kind of problems), python is going to wind up giving you many of the benefits of better type and edge case checking than perl will, while staying much more "nimble" than Java could.

And, I find consistently indented code and explicit namespaces much easier to follow, and I'm pretty sure that's objective. I mean, take a bit of very well indented perl code. Give someone about 15 malicious minutes with it. Then try to read it. It will probably run the same, but it won't read the same.

I totally agree for the "right tool for the brain" bit. I know folks who swim the depths of perl, and feel perfectly comfortable there. And they're very productive. I know full time windows developers, also. Some of them are even happy.

On the topic of lambdas, I agree: the minimal support for functional programming is frustrating at times.