Classes and Objects
What is object?
Objects are an encapsulation of variables and functions into a single entity. Objects get their variables and functions from classes. Classes are essentially a template to create your objects.
A very basic class would look something like this:
Accessing Object Variables
Objectname.variablename
Accessing Object Functions
Objectname.functionname()
Recursive Functions in Python
Now that we have some intuition about recursion, let’s introduce the formal definition of a recursive function. 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.
Python Testing with pytest: Fixtures and Coverage
Fixtures
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”.
But in other cases, things are a bit more complex. You’ll want to have some objects available to all of your tests. 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, and they take a variety of different forms.
In pytest, you define fixtures using a combination of the pytest.fixture decorator, along with a function definition. For example, say you have a file that returns a list of lines from a file, in which each line is reversed:
def reverse_lines(f):
return [one_line.rstrip()[::-1] + '\n'
for one_line in f]
Coverage
This is all great, but if you’ve ever done any testing, you know there’s always the question of how thoroughly you have tested your code. After all, let’s say you’ve written five functions, and that you’ve written tests for all of them. Can you be sure you’ve actually tested all of the possible paths through those functions?
For example, let’s assume you have a very strange function, only_odd_mul, which multiplies only odd numbers:
def only_odd_mul(x, y):
if x%2 and y%2:
return x * y
else:
raise NoEvenNumbersHereException(f'{x} and/or {y}
↪not odd')
Here's a test you can run on it:
def test_odd_numbers():
assert only_odd_mul(3, 5) == 15