0

I have a large class that looks like the following:

class Trainer:
    def __init__(self, name, age, height, weight):
        self.name = name
        self.age = age
        self.height = height
        self.weight = weight
    
    def fit(self, dataloader):
        ....DO MODEL TRAINING...
        
        self.save(path=xxx)
        self.load(path=xxx)
    
    def save(self, path):
        self.model.eval()
        torch.save(self.model.state_dict(), path)
    
    @staticmethod
    def load(path: str):
        """Load a model checkpoint from the given path."""
        checkpoint = torch.load(path, map_location=torch.device("cpu"))
        return checkpoint

From here, I see that since my load() does not need self since in the load method, we do not call self, then we should use staticmethod. Is this correct?

ilovewt
  • 911
  • 2
  • 10
  • 18

1 Answers1

0

Yes you can use static method here. To use static method we don't need to pass class instance, self argument to work upon. Static methods are like , they are just independent of class's instance. And they can be called directly via Class_name.static_method_name without creating the instance to access the method.

Some more readings here

John Byro
  • 674
  • 3
  • 13