Classes and Objects
- 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.
sample class and object:
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)
output:
blah
yackity
access object functions using notation way:myobjectx.function()
Thinking Recursively in Python
"Problems (in life and also in computer science) can often seem big and scary.
But if we keep chipping away at them, more often than not we can break them down into
smaller chunks trivial enough to solve. This is the essence of thinking recursively."
-
A recursive function is a function defined in terms of itself via self-referential expressions.
- If the current problem represents a simple case, solve it.
- If not, divide it into subproblems and apply the same strategy to them.
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:
- 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
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 is an example of a recursive data structure.
- Recursive data structures and recursive functions go together like bread and butter.
5 steps to train yourself thinking recursively
- 1- Use the iterable-approach first.
- 2- Extract the parameters of your function.
- 3- Deduct minimal problem instance.
- 4- Add the solution to the minimal problem instance.
- 5- Expand your function.
Pytest Fixtures
In testing, a fixture provides a defined, reliable and consistent context for the tests. This could include environment (for example a database configured with known parameters) or content (such as a dataset).
Fixtures define the steps and data that constitute the arrange phase of a test (see Anatomy of a test). In pytest, they are functions you define that serve this purpose. They can also be used to define a test’s act phase; this is a powerful technique for designing more complex tests.
The services, state, or other operating environments set up by fixtures are accessed by test functions through arguments. For each fixture used by a test function there is typically a parameter (named after the fixture) in the test function’s definition