views:

278

answers:

5

Perl habits die hard. Variable declaration, scoping, global/local is different between the 2 languages. Is there a set of recommended python language idioms that will render the transition from perl coding to python coding less painful.

Subtle variable misspelling can waste an extraordinary amount of time.

I understand the variable declaration issue is quasi-religious among python folks I'm not arguing for language changes or features, just a reliable bridge between the 2 languages that will not cause my perl habits sink my python efforts.

Thanks.

+1  A: 

I like the question, but I don't have any experience in Perl so I'm not sure how to best advise you.

I suggest you do a Google search for "Python idioms". You will find some gems. In particular:

http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html

http://docs.python.org/dev/howto/doanddont.html

http://jaynes.colorado.edu/PythonIdioms.html

As for the variable "declaration" issue, here's my best advice for you:

Remember that in Python, objects have a life of their own, separate from variable names. A variable name is a tag that is bound to an object. At any time, you may rebind the name to a different object, perhaps of a completely different type. Thus, this is perfectly legal:

x = 1    # bind x to integer, value == 1
x = "1"  # bind x to string, value is "1"

Python is in fact strongly typed; try executing the code 1 + "1" and see how well it works, if you don't believe me. The integer object with value 1 does not accept addition of a string value, in the absence of explicit type coercion. So Python names never ever have sigil characters that flag properties of the variable; that's just not how Python does things. Any legal identifier name could be bound to any Python object of any type.

steveha
"rebind the name to a different object"... Perl OO system was taken from Python, so it is possible to do in Perl.
Alexandr Ciornii
As I said, I have no real experience in Perl, so I don't know anything about the Perl OO system. But I'm curious: I thought in Perl that there were "sigils" that told you something about the type of the variable. For example, @x is an array. You can't rebind "@x" to mean something that isn't an array, right? In Python there are no sigils, and that is a difference I thought worth commenting upon.
steveha
@steveha: In perl, you can have $x, @x, %x, and *x, all different types with the same name. Not entirely best practice to do that, but it's there if you want it. (But yes, "@x" means array x if that's all you're after).
runrig
+1  A: 

In python $_ does not exist except in the python shell and variables with global scope are frowned upon.

In practice this has two major effects:

  1. In Python you can't use regular expressions as naturally as Perl, s0 matching each iterated $_ and similarly catching matches is more cumbersome
  2. Python functions tend to be called explicitly or have default variables

However these differences are fairly minor when one considers that in Python just about everything becomes a class. When I used to do Perl I thought of "carving"; in Python I rather feel I am "composing".

Python doesn't have the idiomatic richness of Perl and I think it is probably a mistake to attempt to do the translation.

perl() if (there's($#nothing) || $rich =~ /about/);
Glenn Maynard
A: 

Don't mis-type your variable names. Seriously. Use short, easy, descriptive ones, use them locally, and don't rely on the global scope.

If you're doing a larger project that isn't served well by this, use pylint, unit tests and coverage.py to make SURE your code does what you expect.

Copied from a comment in one of the other threads:

"‘strict vars’ is primarily intended to stop typoed references and missed-out ‘my’s from creating accidental globals (well, package variables in Perl terms). This can't happen in Python as bare assignments default to local declaration, and bare unassigned symbols result in an exception."

Paul McMillan
But a typoed assignment can still become a declaration of a dead variable, leading to a logic error but no compile error.
hobbs
short names? but then you have to have comments! (http://xrl.us/bfn45h)
ysth
@hobbs well yes. Use a good ide and use a lint/test cases as i said? If it doesn't work, it fails. Simple. Python doesn't have a "strict" mode, trying to shoehorn one in is breaking the language. If your names are so long you routinely mis-type them, you need to rename them. Also, python is not perl.
Paul McMillan
+2  A: 

Splitting Python classes into separate files (like in Java, one class per file) helps find scoping problems, although this is not idiomatic python (that is, not pythonic).

I have been writing python after much perl and found this from tchrist to be useful, even though it is old:

http://linuxmafia.com/faq/Devtools/python-to-perl-conversions.html

Getting used to doing without perl's most excellent variable scoping has been the second most difficult issue with my perl->python transition. The first is obvious if you have much perl: CPAN.

Tom Anderson
I take it you don't know about pypi.
Paul McMillan
I know pypi and can compare it with CPAN. On CPAN I find a large percentage of my projects already coded there, because it has what I need. In pypi I find great stuff, but for me it is a smaller percentage. It depends on the project. For interfaces, text processing, and the general process of data transformation, I prefer Perl/CPAN. For numerical algorithms, logistics, and integration with other languages I prefer Python/pypi. My latest image processing is Python, my latest CAD tool integration is Perl. I choose the language and library on a project-by-project basis. Also C++, vbs, whatever.
Tom Anderson
+1  A: 

Read, understand, follow, and love PEP 8, which details the style guidelines for everything about Python.

Seriously, if you want to know about the recommended idioms and habits of Python, that's the source.

Gabriel Hurley