How animation.FuncAnimation handle **kwargs?

First, in class FuncAnimation(TimedAnimation):

def __init__(self, fig, func, frames=None, init_func=None, fargs=None,
                 save_count=None, *, cache_frame_data=True, **kwargs):
    TimedAnimation.__init__(self, fig, **kwargs)

The **kwargs passed to TimedAnimation.

Then, in class TimedAnimation(Animation):

def __init__(self, fig, interval=200, repeat_delay=None, repeat=True,
                 event_source=None, *args, **kwargs):
    Animation.__init__(self, fig, event_source=event_source,
                           *args, **kwargs)

The **kwargs passed to Animation.

But in class Animation(object):

def __init__(self, fig, event_source=None, blit=False):

Where is FuncAnimation’s **kwargs?

**kwargs in a function signature collects any keyword arguments passed to the function call that are named in the signature. Those keywords and values are then available to your function as a dictionary (called kwargs, the special syntax is the **). We can then unpack that dictionary into the super().__init__(...) call to pass those up to the next [1] __init__ in the chain. There, the process repeats, it picks off the keywords it knows about, and passes the rest along until we hit the base class which if it is passed any extra keyword arguments will raise an exception.

Thus, if you pass the keyword repeat_delay into FunctionAnimation it will pass through FunctionAnimation and be used by the TimedAnimation __init__, if you pass in blit=True it will pass all the way through to Animation and if you pass in does_not_exist='bob' it will raise out of Animation.__init__.

It might be instructive to play with the following code

def funcC(c=None):
     print("at the bottom in C")

def funcB(b=None, **kwargs):
     print(f'in B: consumed b={b}')
     print(f'in B: passing on {kwargs}')
     return funcC(**kwargs)

def funcA(a=None, **kwargs):
    print(f'in A: consumed {a}')
    print(f'in B: passing on {kwargs}')
    return funcB(b=None, **kwargs)

passing different kwargs into funcA.

[1] for more details on super see

Thank you for your reply.

Does it means if I want to pass extra arguments like dose_not_exist to animation.FuncAnimation I must use fargs?

fargs is passed as additional input to the func you provide to FunctionAnimation.

Do you have a concrete example that is motivating your question? I am not sure I fully understand it.

def draw_barchart(time,*frags):
    draw one frame
    ax = frags[0]
    title = frags[1]

def draw_animatie(frames,title):
    draw all frames 
    fig,ax = plt.subplots()
    animator = animation.FuncAnimation(fig, draw_barchart, frames=frames,fargs = (ax,title))

Here is my code,I need to pass ax and title to draw_barchart.

In the beginning, I want to pass them by using **kwargs but I realized it will raise Exception. So I use fargs insetad, It works.


    ax = frags[0]
    title = frags[1]

looks like not a good way to get the arguments.