reading-notes

Classes & Objects, Thinking Recursively, and Pytest Fixtures & Coverage

Classes & Objects

init()

class Vehicle:
    name = ""
    kind = "car"
    color = ""
    value = 100.00
    def description(self):
        desc_str = "%s is a %s %s worth $%.2f." % (self.name, self.color, self.kind, self.value)
        return desc_str

car1 = Vehicle()
car1.name = "Fer"
car1.color = "red"
car1.kind = "convertible"
car1.value = 60000.00

print(car1.description())

Source: https://www.learnpython.org/en/Classes_and_Objects


Thinking Recursively

Maintaining State

When dealing with recursive functions, keep in mind that each recursive call has its own execution context, so 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.

Recursive Data Structures in Python

A data structure is recursive if it can be defined in terms of a smaller version of itself. A list, set, tree, and dictionary are all examples of a recursive data structure.

Source: https://realpython.com/python-thinking-recursively/


Pytest Fixtures & Coverage

Fixtures

@pytest.fixture
def simple_file():
   return StringIO('\n'.join(['abc', 'def', 'ghi', 'jkl']))
@pytest.fixture(scope='module')
def simple_file():
   return StringIO('\n'.join(['abc', 'def', 'ghi', 'jkl']))

Coverage

Source: https://www.linuxjournal.com/content/python-testing-pytest-fixtures-and-coverage