Python for Perl programmers?

Sam Holden sholden at flexal.cs.usyd.edu.au
Tue Sep 21 05:43:15 CEST 2004


On 20 Sep 2004 19:20:07 -0700, Markus Dehmann <markus.cl at gmx.de> wrote:
> I am using perl for everything, even bigger programs, with objects,
> uh, modules and stuff. I know pretty much every trick in perl and have
> a lot of experience.
>
> But I'd like to try a cleaner language, where you don't have to type
> so much crap to create a class etc. So, I wanna give python a try.

Reduced typing is a strange reason to go from perl->python :)

package Foo;
sub new {
	my ($package, $start) = @_;
	bless {count => $start}, $package;
}
sub inc {
	$_[0]->{count}++;
}
sub dec {
	$_[0]->{count}--;
}
sub val {
	$_[0]->{count};
}


class Foo:
	def __init__(self, start=0):
		self.count = start
	def inc(self):
		self.count += 1
	def dec(self):
		self.count -= 1
	def val(self):
		return self.count

Both seem about the same amount of typing. The perl version actually
returns the previous counter value in inc() and dec(), but that's
not that useful anyway.

I guess you could argue all those {}s are "crap" :)
		
> Is there a tutorial that takes all the standard perl things and then
> explains how to do them in python? That would be perfect. Open a file,
> take all the words, put them in a hash, do something with them, print
> the result in a formatted way, write it to a new file etc. Create a
> class that downloads newsgroups, etc. Things like that.

For a smaller grain size, there is:

	http://www.python.org/moin/PerlPhrasebook

Though the perl code has parenthesis around function calls which
I don't think anyone would actually use in practice, eg.

	print (join (" ", @$l));
	print ("\n");

which would usually be (in perl):

	print join " ", @$l;
	print "\n";

or even (for the brave and foolish):

	print "@$l\n";

But other than making the perl code harder to grok for perl programmers
not much harm is done, and it's still understandable and a reasonable
comparison.

-- 
Sam Holden



More information about the Python-list mailing list