Basic syntax
class MyClass:
variable = "blah"
def function(self):
print("This is a message inside the class.")
Can change the value of variables within a class by referencing them using .dot notation
myobjectx = MyClass()
myobjecty = MyClass()
myobjecty.variable = 'ahhhh'
print(myobjectx.variable)
print(myobjecty.variable)
"""
prints:
blah
ahhhh
"""
Can also access contained functions via .dot notation as well
myobjectx.function()
"""
prints
"This is a message from inside the class."
"""
Threading example summing values 1 to 11
def sum_recursive(current_number, accumulated_sum):
# Base case
# Return the final state
if current_number == 11:
return accumulated_sum
# Recursive case
# Thread the state through the recursive call
else:
return sum_recursive(current_number + 1, accumulated_sum + current_number)
Global scope example
# Global mutable state
current_number = 1
accumulated_sum = 0
def sum_recursive():
global current_number
global accumulated_sum
# Base case
if current_number == 11:
return accumulated_sum
# Recursive case
else:
accumulated_sum = accumulated_sum + current_number
current_number = current_number + 1
return sum_recursive()
@pytest.fixture
decorator followed by a function definition