Cheat Sheets - Python / by Tacuma Solomon

Because I tend to forget sometimes!

If you wanna write a simple python script:

https://www.guru99.com/learn-python-main-function-with-examples-understand-main.html

Okay, so modules are what python files are called.

if __name__ == ‘__main__’:

This is basically the signifier for the main function in a python program. Why?

When a module run as a main, the __name__ function has the value of ‘__main__’.

If however, the module is added to another module as an import, the __name__ is equal to whatever the name of the import is. Tada!

From and import statements:

What is up with that? What’s the difference?

  • So if you use import x, then you can refer to all of the things in x in the form of x.something()

  • If you use from x import * you may be in trouble because then you can refer to everything inside of x as its own name but it imports all of the names into the namespace, giving rise to potential conflicts with we have enough of these kinds of imports

  • If we use from x import something, then all that is imported is the module named something, which can be referred to by just its name. This is probably best because you know exactly what you’re importing, and it’s easier to avoid conflicts

What is the deal with *args and **kwargs?

I know the first represents arguments and the second is keyword arguments. What else?

https://pythontips.com/2013/08/04/args-and-kwargs-in-python-explained/

It just allows you to print a variable number of arguments when calling a function; kwargs allows you to call an unlimited number of keywords arguments. It also holds those variable arguments in a list

Parameter vs Argument

Parameters represent variables in a method definition,

eg. Def test_method(parameter1, parameter2):

print(something)

Arguments represent the data that is passed into a method once it is called:

test_method(a,b)

What is __repr__

https://docs.python.org/3/library/functions.html#repr

Apparently it is a method of representing an object as a string. Should generate the same value when passed into Eval() as a regular object.

What is __add__

It is basically a class method that describes how two objects are added.

Python Logging:

Excellent Guide:

https://www.datadoghq.com/blog/python-logging-best-practices/#unify-all-your-python-logs

Customizing Warnings:

https://docs.python.org/3/library/warnings.html

Python Logging Cookbook:

https://docs.python.org/3/howto/logging-cookbook.html#logging-to-multiple-destinations

(Especially logging to different places at the same time)

Breaking up long-ass lines in Python:

https://doingmathwithpython.github.io/breaking-long-lines-in-python.html

https://stackoverflow.com/questions/15666030/when-is-a-python-operator-a-line-continuation

https://stackoverflow.com/questions/5809059/line-continuation-for-list-comprehensions-or-generator-expressions-in-python

https://www.pythonforbeginners.com/basics/list-comprehensions-in-python

Importing modules:

https://www.digitalocean.com/community/tutorials/how-to-write-modules-in-python-3

Method Overriding

https://www.thedigitalcatonline.com/blog/2014/05/19/method-overriding-in-python/

Dealing with Pyenv

https://amaral.northwestern.edu/resources/guides/pyenv-tutorial

https://realpython.com/python-virtual-environments-a-primer/

Error handling and exceptions:

https://www.pythonforbeginners.com/error-handling/python-try-and-except

F-strings:

https://realpython.com/python-f-strings/

python-logo-master-v3-TM.png