Search code examples
pythontorchkeyword-argument

How does "kwargs" is passed to a variable implicitly?


A simple code from pytorch tutorial, which is used to load the data ''' tr_data = datasets.FashionMNIST(root="data", train=True, download=True, transform=ToTensor()) '''

enter image description here

However, when debug into the code as shown above. It use the "new" method without passing the "kwds". It seems the param "transform=ToTensor()" is never used, which in the result is not true. So I am wondering how the code processes the parameter, where-else is the kwds is read into a variable.


Solution

  • It's checking to see if the superclass's __new__ method is the same as object's. This can happen two ways: the superclass is object, or the superclass hasn't overridden __new__ and neither have any of its superclasses back to object.

    object doesn't take any arguments in its constructor, so it doesn't pass them if that's the case. Since object doesn't take any arguments, it also doesn't need any, so the superclass call creates the object correctly.

    After __new__ is called, __init__ is called. That is what actually initializes the object (as opposed to creating it). It needs to do something with the arguments, and likely does.