reading-notes

Classes and Objects

Objects are an encapsulation of variables and functions into a single entity Classes are essentially a template to create your objects.

class MyClass:
    variable = "blah"

    def function(self):
        print("This is a message inside the class.")

Accessing Object Variables

To access the variable inside of the newly created object

class MyClass:
    variable = "blah"

    def function(self):
        print("This is a message inside the class.")

myobjectx = MyClass()

myobjectx.variable

Accessing Object Functions

To access a function inside of an object you use notation similar to accessing a variable:

class MyClass:
    variable = "blah"

    def function(self):
        print("This is a message inside the class.")

myobjectx = MyClass()

myobjectx.function()

Thinking Recursively in Python

Dear Pythonic Santa Claus…

algorithm for delivering presents is based on an explicit loop construction, it is called an iterative algorithm.

houses = ["Eric's house", "Kenny's house", "Kyle's house", "Stan's house"]

def deliver_presents_iteratively():
    for house in houses:
        print("Delivering presents to", house)

algorithm with which he can divide the work of delivering presents among his elves:

Recursive Functions in Python

A recursive function is a function defined in terms of itself via self-referential expressions.

All recursive functions share a common structure made up of two parts: base case and recursive case.

def factorial_recursive(n):
    # Base case: 1! = 1
    if n == 1:
        return 1

    # Recursive case: n! = n * (n-1)!
    else:
        return n * factorial_recursive(n-1)

Maintaining State

to maintain state during recursion you have to either:

1- Thread the state through each recursive call so that the current state is part of the current call’s execution context

2- Keep the state in global scope

Pytest Fixtures and Coverage

Fixtures

Those objects might contain data you want to share across tests, or they might involve the network or filesystem. These are often known as “fixtures” in the testing world,

def reverse_lines(f):
   return [one_line.rstrip()[::-1] + '\n'
           for one_line in f]

Coverage

you’ll get a coverage report for every part of the Python library that your program used, so I strongly suggest you provide an argument to –cov