Five Things I Wish I Knew When Starting with Python

 

There are definitely more than five things, but here are five that really struck me.


1. Create specific environment for project and only install the required packages


By creating a virtual environment, Python libraries specific to a project remain within that environment. Any dependencies(packages) installed in one environment is isolated from another. This might prevent version clashes between packages or unknowingly using a different package version across projects.

Creating project specific environments is good house-keeping. This should be done even before  any code gets written. 

You can create a virtual environment with Python Virtual Environment or with Conda. The example below is listing all available conda environments and creating a new one with conda create.

Tangibly, the environment is a folder location on a drive. That you can activate with a command to manage




2. List/Dictionary Comprehensions 

What would life be without List/Dictionary Comprehensions? It allows the use of one line of code to evaluate a loop and execute a conditional statement to filter values from an iterable.

Must admit it took awhile to wrap my head around the syntax, but it has helped to reduce number of lines of code. 
 

See below examples. Between a and b are comparisons for a single iterable. For d and e, it demonstrates how comprehensions can make your code more concise with nested iterables.







3. Python is a dynamically typed language

This means that the type of value passed into a function is determined at run time. It is also the nature of Python being an interpreted language. Unlike other static typed language programs, where the type must be declared before-hand for compilation to work. Compiled code can then be run.

That freedom  can also lead to issues like a lot of run-time type errors, if one is not careful. Personally, I find a Test-Driven Development approach useful for Python and unit tests are life savers when it comes to dynamically typed language

Seems obvious, but when starting out, you don't know what you don't know.


4. Using introspection

Knowing introspection commands can help you debug issues. They can be used to prevent runtime errors too by checking the type, well, at runtime. Commands like type(), isinstance(), issubclass(). As mentioned in point 3., Python is a dynamically typed language and sometime bugs happen due to type errors. It is a terrible thing to start out in Python, build stuff and have all these type errors at runtime. Would argue that introspection is a subtly important aspect of Python.


5. Using  if __name__ = "main": 

So you have your  written your first .py file but it is executing when you import it into another module and it is driving you mad, because you don't want that. You just want to use, say a function in the module.

Using the if __name__ = "main": statement at the end of a .py file does 2 things. First, it executes as a script if .py file is invoked at the interpreter. Secondly, if .py file gets imported into another code session  as module, no code will execute but the session gains the functionality of .py 

There are a lot good knowledge articles that delve into the details. 



Comments

Popular posts from this blog

Test Driven Development: How and Why

Using Git pre-commit hooks: Automated Testing of Code with Python Unit Tests