Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Managing Perl habits in a Python environment

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.

like image 978
namrokretep Avatar asked Sep 28 '09 21:09

namrokretep


2 Answers

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.

like image 116
Tom Anderson Avatar answered Sep 28 '22 00:09

Tom Anderson


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.

like image 29
steveha Avatar answered Sep 28 '22 02:09

steveha