Skip to content

Latest commit

 

History

History
 
 

Material_Part5_BetterCoding

Better coding

Why you should run code in your module only if __name__ is "__main__"

Always encapsulate your module's content in functions/classes and have all function calls and instantiations contained after the statement

if __name__ == "__main__"

Why? Because, if you import your code as a module, all code that is not contained in a function or class will be run. Now, if you directly run the module,__name__ will be set to "__main__". But if the module is imported, __name__ is set to the module's name. See the examples module1.py and module2.py.

Task 1: Run the code in module1.py and module2.py and see what is run upon import and how __name__ changes.

Demonstrations

I will demonstrate how to improve the readability in some code snippets.

Further examples

Here are further examples, loosely based on this video. Feel free to take a look if any of these suggestions may help you write better code in the future. Please not that brevity has its limit and sometimes using more lines to write a piece of code can actually be better: If it helps to improve understanding of the code.

Ternary conditionals

Use ternary conditionals to simplify if ... else statements. Example

Context managers

Use context managers to not have to worry about tear-down methods, like closing a file. Using with open, for example, automatically closes the file after it has been read. Example

Using enumerate

Use enumerate when you want to access both the list item and its index at the same time when iterating. Example

Using zip

Use zip if you want to iterate over two lists simultaneously. Example