Essential Reads for Any Python Programmer

These days I’ve seen people writing code in Python with many many different backgrounds. Python is a well known language with small footprint and it has a great community. All of which makes it a great language for beginners and also very easy to jump in to and start your project. But, also extremely easy to miss important knowledge about underlying structure and design pattern conventions.

So I’ve started using Python for a couple of my bigger projects and especially my research now. One of these projects demands some how a more rigorous understanding of concepts and underlying structures of the language. And in general I try to use appropriate or conventional software design patterns used by the community. Not that I’m always completely successful in it but I try.

During the past few weeks I have came by interesting or must read articles that I suggest anyone who already writes code in Python or is planning to do so to take a look at. Especially if you are coming from other programming languages.

If you find my post useful please let me know in the comments. It will definitely cheer my up. If you have suggestions that need to be added to the list. Please, share them with me through the comments. I would love to both read them and share them.

Note: I’m assuming you have already reviewed tutorials for writing basic codes in Python and you are generally familiar with the standard concepts Object Oriented programming.

Code Style Conventions

Ok this is probably a boring start to the list. It is perhaps not a hidden gem to anyone. But, I’ve seen too many people not following the coding style conventions. I know it is your code, but if you are planning to share it any time in future, you probably want others to like it and find it useful. I think one of the most important factors of an open source project is readability. And perhaps the very first step to having a readable is to follow the conventions and styles that people are used to. This is probably the only place that you don’t want to be super creative.

It is always easy to dump all your codes and modules in a directory and run them when it comes to Python. But, if you are planning to have publish your open source project in future it is probably a good idea to start right from the beginning.

Python Modules, Packages, Classes, Functions and Methods and When to Use Them

I guess you might think these are really basic stuff. But, I think it is different enough from what these things represent in other programming languages. For example, I find it easy to make simple mistakes in using them appropriately. While this won’t necessary make your code crash, yet it impacts the readability of your code.

Basically don’t use modules for every individual class. Think of what you do in Java and then don’t do it (;)) No but seriously, you don’t want to create one module per class. A module will contain all the related classes, functions and other types and definitions that are related. Similar to what a package would do for you in Java.

Now, packages are some how another layer that allows you to further structure your projects. But, you are some how discouraged from creating too many packages in Python. I guess you would use packages when:

You are working on a very large project

You are working on a projects with multiple sub-projects and you want to structure the package structure of all projects with a specific pattern

Libraries on the other hand are some how a collections of code (classes, modules and packages) that are supposed to do something. Most commonly published for others to use.

I believe classes are the units of object oriented programming in Python. I would like to say that a class’s concept in Python is very similar to the concept known in most other object oriented languages like Java. However, a few interesting articles to read

A piece of code that can be called and might accept arguments. A function is a independent unit of code segment in Python and doesn’t necessary belong to class, object or another entity. You can import a function from another module in Python.

A method is a piece of code or if I may a function that belongs to an object or class.

Public, Protected and Private in Python

Ok, at least for me it was a bit of a shock coming from Java. But, you may want to specifically know about name mangling and how it works as a kind of means to create private variables in Python. I’ve encountered many articles that don’t mention them and even people who have been writing code in Python and are not familiar with it

You might feel this is a bit too much detail. But, I personally found the information really interesting and useful to have a clear understanding on what the hell is going on when you define a class and instantiate it:

I don’t think duck typing is specific to python. In contrary it is probably a common concept in most dynamically typed languages but it has become a famous concept in Python. If you are coming from a background of statically typed language like Java, it is important to read about it. It really thinks once you know it you would feel way more comfortable with Python.