DEV Community

Connor
Connor

Posted on

Python for C# devs

Photo by Maxwell Nelson on Unsplash.

Recently I've been using python for most of my personal projects rather than C#. I really like C#, its been my main programming language for quite some time. However, for many things, you really don't need all the bulk surrounding a C# binary. Therefore, for more simple projects I really like Python.

However, I have only really started to become proficient in the language until very recently. Part of the issue was my history with Python, coming from a more... pure OOP experience with C++ and C#. However, after some more experience, Python is quickly becoming my go to language for simple scripting and medium sized projects.

OOP and Me

Initially, Python doesn't seem to be as robust (OOP wise) as something like C++, however, believe it or not, Python is actually totally object oriented. Admittedly, this fact escaped me for an embarrassingly long time, thinking Python was more like Javascript than C#. Though the dynamic typing is quite strange, once you realize that basically everything is an object, including functions themselves, you can start to do really powerful things.

For example, when starting to write my library for interacting with the Guild Wars 2 API, I discovered Python's decorators. Basically, because everything is an object, including functions, we can wrap functions with others, enabling runtime checking of conditions. For example, in the API, some endpoints require a API token to be provided. We can check, before we call the API, if the user has included an API token only on the endpoints that require it. All we have to do is define and add the decorator @requires_auth before the function, and, Viola! We can now enforce endpoints to require tokens to be given. If a new endpoint is added, we can just create the function and add the decorator in one easy to understand line.

There is a great article on this, I'll link here here for further reading: https://www.programiz.com/python-programming/decorator

XKCD

Up for Interpretation

The other big thing I am liking more and more about Python is, because it is interpreted, no compilation is required. This makes quickly making a script to, say, scrape data from a website hilariously quick and easy. However, this has the obvious drawback of not being compiled, where we can enforce things before we actually run. Usually we use PyLint, however PyLint was really giving me some headaches when working on my custom module, but its something you have to just deal with unfortunately.

One other thing I'd like to add is that at first I, naively, thought there was no way to enforce types of parameters into functions (and even then I'm not sure it will truly "enforce" it but, oh well). Usually most tutorials online will simply omit this. However, simply adding a : type after a parameter will enforce its type, and will allow you to use intelligence if you're using VSCode. Which is really handy. Here's a quick example:

def func(items: []):
    for i in items:
        print(i)
Enter fullscreen mode Exit fullscreen mode

adding : [] enforces the input type. Neat.

Again, all this may sound simplistic, but switching languages is always a challenge.

One qualm so far with all of this, and its not even a Python thing, is using VIM and VSCode autocomplete. Whenever I want to use a template, like the def template, whenever I hit tab it puts me into visual select mode instead of insert. If there was a way to change this I would be very appreciative of it. I have been doing c and just deleting everything and moving on but there must be a better way to move around that.

Thanks for reading!

Top comments (0)