I need some explain

I don’t know the For statement, Why ax in axs can plot while ax is set = None

In Python, optional arguments’ default values can be overridden by simply passing in the desired new value to that argument. Take, for example,

def f(x=0):

When we do f(), the output would be 0 since that is the default value of x. However, if we execute f(10), we would get 10 because 10 is essentially replacing 0 as x's value.

Here during the for loop, when show_preds is called, ax isn’t None because we are overriding it with a subplot belonging to axs (i.e. the iterator ax). But notice that show_preds creates ax a subplot in case it’s None, meaning even if we didn’t override its default value, the data would still be displayed, although no more than one subplot would be shown in each figure.

Does that answer your question?

thank you

1 Like

Can you explain more ? I know ax is override but when ax != None , how the If statement work?

The if, which is,

if ax is None:
        ax = plt.subplots()[1]

does not get triggered because ax is, well, not None; it’s an AxesSubplot object.

Another way to look at it is that is and == are identical in this case (although as a general rule of thumb, the former should be used), so the above is equivelant to,

if ax == None:
        ax = plt.subplots()[1]

As you mentioned, ax != None, thus we clearly don’t enter the if.

Please do let me know if you needed me to elaborate more.

I got this. Thanks a lot

1 Like