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.
- PEP 8 — Style Guide for Python Code
- The Hitchhicker’s guide to Python Code Style (for a shorter review)
Structuring Python Projects
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.
- The Hitchhicker’s guide to Structuring Your Project
- Marko Klemetti’s article about “Python, creating your project structure”
- An interesting Stackoverflow thread
- Importing packages with overlapping package hierarchies in Python (My personal notes from working on a larger project with multiple sub-projects)
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.
- Referenced Documentation for Modules
- Modules vs Packages vs Libraries in Python by Bhaskar (quick read)
- An interesting Stackoverflow thread and another quick read
My takes on this from here and there:
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
- Private, protected and public in Python (However, in contrary to what Radek says the single _ is also a way of defining private variables in Python as mentioned in the official documentation)
- Official documentation on private variables
Python Class vs. Instance variables
Definitely don’t skip this. I posted link to articles for Python2.7 and 3x but I don’t think there are significant changes.
- Class and instance variables in Python 2.7
- Python 3 class and instance attributes
- Understanding Class and Instance Variables in Python 3
Old/Classic style classes Vs. New Style Classes
While we are on the sweet subject of classes, lets take a look at what do people mean when they keep saying Old style classes or New style classes
- NewClassVsClassicClass (from python’s wiki)
- The Inside Story on New-Style Classes
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:
More on Python Classes:
Some other interesting articles that I have encountered:
- Efficiently Implementing Python Objects With Maps
- Improve Your Python: Python Classes and Object Oriented Programming (interesting read)
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.
- Duck Typing in Python
- Python duck typing (or automatic interfaces)
- What is duck typing by Eric Lippert( Interesting read, don’t miss it)
- Duck Typing by Phil Haack (Another interesting article arguing with Eric Lippert point on the matter)
- Java to Python? Learn to think differently about type checking! (My comments from a personal experience! If you find examples useful I think this might be interesting to you)
Interesting collection of hints for Python
Found this link in one of the articles. It just seems like a really long list of great hints for python programmers. I haven’t read all of it yet but its definitely a great read so far.
3 comments On Essential Reads for Any Python Programmer
Anyone looking for more information on Python 2 vs Python 3, I recommend that you check out this post: https://www.activestate.com/blog/2017/01/python-3-vs-python-2-its-different-time
thanks for sharing nice information…….
That’s a real helpful guideline to be pro in Python
Thanks a lot!