what is the benefit of making a class with only staticmethod?

Viewed 21

I'm working in a new place and they have an Utils class (made by somewhone who left) that look like that

class Utils(object):

    session: Session = get_base_session()
    
    def __init__(self, session: Optional[Session] = None) -> None:
        Utils.update_utils(session=session)

    @staticmethod
    def update_utils(session: Optional[Session] = None) -> None:
        if session:
            Utils.session = session
    
    @staticmethod
    def a_random_method():
        return Utils.session.post(url="xxxx", headers=headers, json=password)

why? why is there a constructor if there is only staticmethods inside? (which can be called without instanciating the class)

Is there a benefit? doing a git grep I found no place where the class is instanciated, so the constructor is never be used, I guess.

Also I don't understand how session could hold any value since it's not instanciated

I would have done it like that :

class Utils:
    
    def __init__(self, session: Optional[Session] = None) -> None:
        self.session = None
        self.update_utils(session)

    def update_utils(self, session: Optional[Session] = None) -> None:
        if session:
            self.session = session
    
    def a_random_method(self):
        return self.session.post(url="xxxx", headers=headers, json=password)

instanciate it and use it.

I'm not asking about "in your opinion which is better" I'm asking if objectively there is a good reason to go with one or the other, or if it's just personal taste.

Thanks.

0 Answers
Related