PowerShell to Python And Back
This article was originally posted on the IpSwitch.com blog.
If you’ve read any of my previous articles, you will understand that I’m a huge PowerShell guy. I’ve been working with PowerShell for nearly ten years now and with the latest 2 of those years have been primarily a PowerShell developer writing PowerShell code full-time.
I love PowerShell and automation, and if you’re in charge of automating any kind of Microsoft product, you probably know a thing or two about PowerShell too. But, PowerShell is not the only game in town; enter Python.
People inside of the Microsoft community (which I’m a part of) love PowerShell and use it every day, but there’s an entirely new world out there in open-source land. The Linux world is also a thriving community full of great operating systems, tools and products to manage things. In Linux land, when it comes to scripting, PowerShell is the weakling on the block. The big boys of scripting in Linux are Python and Perl. Although PowerShell and these languages are all technically cross platform, it’s clear that each language has its dominant audience.
In the next few articles in this series, I will be taking you on my personal journey being a PowerShell “expert” to being completely new at another language; Python, in this example. Why Python? I’m choosing Python because it’s the fastest growing language. It’s a programming language that looks and acts like a scripting language. Unlike lower level languages like C, C++, Java and others, to me, it doesn’t feel like “coding”. It feels like I’m in PowerShell scripting away. From someone that still has a hard time defining myself as a “developer”, this is critical. I’m not concerned with how to make the most efficient use of memory and CPU. I don’t care about pointers, registers and everything under the hood. I just want to automate! Python seems to be the best choice.
In the short time I’ve been using Python, I’ve discovered that there are many principles that easily carry over from one language to another. For example, nearly all programming and “scripting” languages have constructs like if statements, loops, variables, functions, objects, etc. Understanding the basic foundation of code and how to write general code will give you a major leg up in learning a new language. Once you can grasp these basic concepts, this knowledge will transfer over to lots of other languages.
Once you’ve got the basic concepts of code down, the next step is to learn the “best practice” way to perform a task in that language. For example, in PowerShell, it’s common to perform tests up front on function parameters via parameter validation. I’ve recently learned that in Python, no validation is typically performed. Instead, coders enclose the code inside of a try/catch block, catch the exception and deal with it then. This validation is just one, tiny example of what “Pythonistas” call “pythonic” code.
When I started with PowerShell, I was coming from a primarily PHP background. I was ending lines with semicolons, using PowerShell aliases like echo to send output to the console and so on. Even though you can make your new language of choice look like another language you’re used to doesn’t mean you should. It’s important to not only learn the syntax but also how to write the code according to agreed upon guidelines in the community.