Reading-Notes

View the Project on GitHub

Classes and objects

Objects are refered to a container that have functions and variables coming from classes.

Classes are templates to create the object

Each object you create in the class will have its own versions/copies of variables and functions.

__init__ : it is a function that is called when the class is being initiated, It’s used for asigning values in a class

More

Thinking Recursively in Python

What is Recursive?

The basic idea around Recursive is breaking problems into subproblems.

Recursive function is a function that calls itself until it reaches the base case (a certain condition), then it returns the result.

All recursive functions has two parts:

Recursive functions add to the Stack each time it is called, and once it reaches the base case it starts to return each call until excecution.

To maintain state during recursion you have to either:

  • Thread the state through each recursive call so that the current state is part of the current call’s execution context
  • Keep the state in global scope 1

This is an example of a recursive function for summing numbers

Recursive data structres in Python

Any data structure that is defined by calling itself is a recursive data structure:

and more

Naive Recursion is Naive

interesting note: Fibonacci was defined to find the number of rabbits as: 2

the number of pairs of rabbits born in a given year is equal to the number of pairs of rabbits born in each of the two previous years, starting from one pair of rabbits in the first year.

More

Pytest Fixtures and Coverage

Pytest is a library in Python used to test codes

Fixtures

A fixture provides a defined, reliable and consistent context for the tests.

We can tell pytest that a particular function is a fixture by decorating it with @pytest.fixture

“parametrized tests”3

When you’re writing tests, you’re rarely going to write just one or two. Rather, you’re going to write an entire “test suite”, with each test aiming to check a different path through your code. In many cases, this means you’ll have a few tests with similar characteristics, something that pytest handles with “parametrized tests”.

Coverage

A library that checka that your tests have run all of the code.

package: pytest-cov.

More

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

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

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