psss...I want to move from Perl to Python

Cameron Simpson cs at zip.com.au
Thu Jan 28 20:20:55 EST 2016


On 28Jan2016 19:01, Fillmore <fillmore_remove at hotmail.com> wrote:
>I learned myself Perl as a scripting language over two decades ago.  All 
>through this time, I would revert to it from time to time whenever I needed 
>some text manipulation and data analysis script.
>
>My problem? maybe I am stupid, but each time I have to go back and 
>re-learn the syntax, the gotchas, the references and the derefercing, 
>the different syntax between Perl 4 and Perl 5, that messy CPAN in 
>which every author seems to have a different ideas of how things 
>should be done....

Yes. CPAN always seemed a royal PITA to me too. Pip and Python virtualenvs are 
much cleaner IMO.

>I look and Python and it looks so much more clean....

Oh yes. I wasted quite a while with Perl before shifting, largely because I had 
a large base of personal Perl modules I used a lot and felt like the bootstrap 
process would be painful. I do wish I'd jumped in a few years earlier.

>Does Python have Regexps?

It has a regexp module named "re" and they're the same syntax as perl. It 
doesn't have regexp operations as part of the language itself (no "=~ /regexp/" 
stuff) - you compile a regexp and then use it. This does mean you use regexps 
more deliberately and less frequently than in Perl, but that is a Good Thing.  
Regexps are far far overused in Perl and elsewhere. They are cumbersome, 
cryptic and error prone. Python's strings have a bunch of fixed-string methods 
available which cover a surprisingly large number of cases before you need the 
complexity of a regexp.

>How was the Python 2.7 vs Python 3.X solved? which version should I go for?

Go for 3. While not everything has been ported, many many things have and you 
will mostly be fine. Python 3 has a much cleaner bytes vs text separation 
(which is also a Good Thing), some library cleanups, and all the new work goes 
to Python 3. Python 2 exists, but is essentially frozen. I try to write code 
which will work in both (easy most of the time), but some of my code will only 
work in Python 3 because it works with bytes and text, and going back to the 
ambiguity of Python 2 would be a disaster (in fact, the module I'm thinking of 
had many potential bugs which got cleaned up as a direct result of the move to 
3).

>Do you think that switching to Python from Perl is a good idea at 45?

Yes. Yes yes yes.

>Where do I get started moving from Perl to Python?

There are tutorials, if that kind of thing works for you. My approach was to 
just start writing small things in Python. And when those things got reused, 
start making them modules (== "packages" in Perl speak). You will end up with a 
little suite of tools.

Also, join the "tutor at python.org" list. That is where to go for new Pythoners 
seeking help.

>which gotchas need I be aware of?

Hard to say, I found it depended on what I was doing.

I think the biggest for all new users is probably the "default argument value" 
semantics. When you write a Python function:

  def foo(a, b):
    ...

you can specify default values. For example:

  def (a, b=3):
    ...

means you can call foo as foo(1) and it will be like calling it as foo(1, 3).  
The important point here is that the default values are computed at function 
definition time, _not_ at function call time. For constants like the above this 
doesn't matter, but consider this:

  def foo(a, b=[]):

so that "b" defaults to being an empty list. Because "[]" is computed when the 
function is defined, you will be reusing the _same_ list whenever you call the 
function without specifying "b"; it won't stay an empty list, becuase it is not 
a _new_ empty list of each call.

So if you had "b.append(4)" inside the function, the list would progressively 
accrue 4s, and so forth. For this reason the standard idiom for default 
arguments goes:

  def foo(a, b=None):
    if b is None:
      b = []

Since that test runs on each call (being inside the function), it does make a 
new empty list for "b" if it was not specified.

Cheers,
Cameron Simpson <cs at zip.com.au>



More information about the Python-list mailing list