Here is an example of using FuncAnimation without a custom class:
import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation def animate(data, im): im.set_data(data) def step(): while True:
When you use yield (as opposed to returning) in a function, it makes the function a generator . Generator functions save state. Each time you call next on the iterator returned by the generator, the thread of execution is selected where it was stopped (from the last yield expression). That's why generators are a way to avoid global bindings - potential global variables are only local variables inside the generator function, and their state is preserved between next calls.
By the way, the warning "never use global variables" is not accurate enough. We constantly use global variables. Each time you import a module at the module level, the module object is global. Each time you define a function or class at the module level, it is global. There is nothing wrong with using globals (although itβs true that accessing global functions inside a function is slower than accessing local functions. However, beware of prior optimization).
Perhaps a warning should be read instead: "Do not try to use global variables that change state." The reason why changing global values ββis bad is because every function that changes the global becomes incoherent. This function can no longer be understood and tested as an isolated unit of code. Primary programming tools are used to solve problems in order to break down big problems into smaller parts. Functions and classes help break down problems into smaller pieces. But when you use mutating globals, you lose this advantage. The mind should now look at the entire module at once to understand the code.
source share