Two questions on programming practices

As I was going through lesson 4, mnist_basics, I found a few things difficult to understand. I think that they are related to programming practices than ML. Thanks in advance.
1)In train_epoch function (link here), model, lr and params are used as arguments, but the DataLoader is not. Since we do use dl within the function, shouldn’t we use it as an input? why do we input lr and params but not dl?
2) We add *args and **kwargs in the definition of methods step and zero_grad under the definition of class BasicOptim (link here). But we never use it. So, what is the reason behind adding them?

strictly speaking yes. tbh jupyter notebooks makes it super easy to throw things together very easily in with a very experimental mindset, and it does tend to lead to bits of code like that where you know you’ve created a variable a couple of cells up.

in the initial stages of kicking things about to see what you want to end up with it’s not a big deal, but if you were intending to reuse that code elsewhere or if you were actually in the process of developing a library of some sort then that’s something you would fix before you exported it.

i’d say the two main reasons you’ll see this (and you’ll see it in various major libraries throughout your career), are

  1. because it’s easy to pass things further down without any effort on your part (eg: all the way down to pytorch potentially). that’s very convenient to you but not great for the programmers who come along afterwards and have to use your code. ask anyone new to python learning matplotlib :upside_down_face:
  2. future proofing. in a properly designed object model of a decent size like fast.ai, you need to bear in mind which parts of it you’re likely to want to change or add to in the future and design things in such a way that you won’t break all the existing code when you do so. if you see args and kwargs in fast.ai 2 it’s going to be the second one.
2 Likes

Thank you so much for your comprehensive answer!

And I remembered to post on forums first this time :upside_down_face: