reading-notes

My reading notes journal for Code Fellows

View the Project on GitHub

Readings: Topic

Classes and Objects

Object are important because we use these to places our variables and functions into a single entity.

Classes are important because we use these to give object their variables and functions.

Example of Class:

class MyClass:
    variable = "blah"

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

Example of how to create multiple Objects:

  class MyClass:
    variable = "blah"

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

        myobjectx = MyClass()
        myobjecty = MyClass()

        myobjecty.variable = "yackity"

        # Then print out both values
        print(myobjectx.variable)
        print(myobjecty.variable)

The init() function, is a special function that is called when the class is being initiated. It’s used for assigning values in a class.

class NumberHolder:

  def __init__(self, number):
      self.number = number

Thinking Recursively

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

This means that the function will continue to call itself and repeat its behavior until some condition is met to return a result. All recursive functions share a common structure made up of two parts: base case and recursive case.


The algorithm for iterative present delivery implemented in Python:

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)

deliver_presents_iteratively() Delivering presents to Eric’s house Delivering presents to Kenny’s house Delivering presents to Kyle’s house Delivering presents to Stan’s house

This is the typical structure of a recursive algorithm. If the current problem represents a simple case, solve it. If not, divide it into subproblems and apply the same strategy to them.

The algorithm for recursive present delivery implemented in Python:

houses = [“Eric’s house”, “Kenny’s house”, “Kyle’s house”, “Stan’s house”]


def deliver_presents_recursively(houses): # Worker elf doing his work if len(houses) == 1: house = houses[0] print(“Delivering presents to”, house)

# Manager elf doing his work
else:
    mid = len(houses) // 2
    first_half = houses[:mid]
    second_half = houses[mid:]

    # Divides his work among two elves
    deliver_presents_recursively(first_half)
    deliver_presents_recursively(second_half)

deliver_presents_recursively(houses) Delivering presents to Eric’s house Delivering presents to Kenny’s house Delivering presents to Kyle’s house Delivering presents to Stan’s house

REFERENCES

https://www.learnpython.org/en/Classes_and_Objects https://realpython.com/python-thinking-recursively/ https://docs.pytest.org/en/latest/explanation/fixtures.html https://www.linuxjournal.com/content/python-testing-pytest-fixtures-and-coverage